Quote:
Originally posted by guest72485
why is that so, i thought static membe variables can be accessed only by static member functions. am i wrong?
|
It's the other way around.
A static member has a lifetime of the entire run-time of the program.
A non-static (auto, register or volatile) member has the same lifetime as the object to which it belongs (from the start of the constructor body after object allocation to the end of the destructor body before object clean-up). If you have multiple objects of the same class, each object gets its own member variable, seperate and independant of the others.
A static method also has a “lifetime” of the entire run-time of the program; it's just a normal method that happens to be in the scope of a class, and subject to class protection rules.
A non-static method is implicitly passed a reference to the object by a hidden parameter (called
this). So you need to have a defined object in order for the method to be called; if no object is defined, then the method can't be called. (So, while you could make
disp non-static, you would need to allocate an object of type
sample in order to call it).
Any method can access a static member variable, assuming that it's got sufficient permissions to do so (
i.e. is in the same class; all member variables should always be declared private).
A non-static member variable can only be accessed from the object to which it belongs. Since a static member does not belong to an object, it can't access non-static variables.
Btw, the current version of Linux is 2.6.something (last time I checked), so I suspect that 6.2 is the version of your distribution. Which doesn't tell us anything because you've not said which distribution you're using.
Hope that helps,
— Robert J. Lee