-
Static Data Member
referring to this - http://publib.boulder.ibm.com/infoce...ef/cplr038.htm
"You must define the static member outside of the class declaration, in namespace scope." - namespace scope?
I tried to define a static member declared in a class in main() , and it gave me error, i also read somewhere that static members should always be defined in global scope.how does this all relate?
-
Code:
class foo
{
public:
static int bar;
};
int foo::bar;
int main()
{
...
}
-
Quote:
Originally Posted by
hk_mp5kpdw
Code:
class foo
{
public:
static int bar;
};
int foo::bar;
int main()
{
...
}
what if i place static int bar in private ?
-
>> what if i place static int bar in private ?
Exact same thing.
>> namespace scope?
Do you know what a namespace is? If not, you have to understand them first. If yes, then you know that you can declare your class inside a namespace. The quote is saying that the static member must be defined inside the scope as the class. If you define it inside the main() function it won't work, that is a deifferent scope than the class is in.
If you aren't using namespaces, then the proper scope to define the static member is the global scope.