private static member variable
definition,
StaffMember::NextSpace.
Static data must be defined, as functions. For functions, it is simple :
you
can define it in the class
class A
{
void f()
{
//
}
};
or outside
class A
{
void f();
}
void A::f()
{
//
}
It is the same with static data :
class A
{
static int i = 10;
};
or
class A
{
static int i;
};
int A::i = 10;
The problem with inclass static data definition is that it works only
for integral types, that is,
class B
{
static float f = 1.0; // wrong
}
is illegal. This should be possible in a future standard. Right now,
you must initialize it outside the class.
But why a different syntax for static data and non-static data? The
thing is, non-static objects are defined and initialized in a constructor,
which solves the problem. Static variables are not bound to an object so
they might not be initialized (or even defined!) if you try to access them,
so you must at least define static variables. You can use the definition
to initialize it at the same time.
Which is rare, thank you.
Is there a difference defining public and private functions outside a class?
No, and it is the same with static data.
Jonathan
|