Can there be a member variable in a class which is not static
but which needs to be defined
(as a static variable is defined for reserving memory)? If so, could I have an example? If not, then why are static members the only definable members?
BJARNE said if you want to use a member as an object ,you must define it.
But my program is showing error when i explicitly define a member variable:
class test{
int i;
int j;
//...
};
int test::i; // error: i is not static member.
In your example, declaring
i
andj
in the class also defines them.See this example:
You can now access
a
by declaring an object of typeFoo
, like this:It's a little different for
b
: Because it isstatic
it the same for all instance ofFoo
:For the above all will print that
b
is 10.