#include <stdio.h>
class C
{
public:
static int i;
static int j;
};
int i = 10;
int C::i = 20;
int C::j = i + 1;
int main ()
{
printf("%d", C::j);
return 0;
}
What is the value of: C::j
I was reading a c++ quiz and came across the following question. I thought that the answer is 11
.
int C::j = i + 1;
Since it is accessing the non static i
which is 10? So, I thought 11
should be the answer?
I compiled and ran this code through visual studio and it prints 21
. Which is confusing to me. Can someone please explain why this is happening? What am I missing?
int C::j = ::i + 1;
. – TriedC
you would not want to typeC::i
every time you meant the member, would you? – Sexennialthis->member
. – SesslerC
is the name of a class, and what I really want is the name of the instance. Sothis->
is...almost what I want, except->
is a lot more awkward than.
. – Paxton