just look at the following C++ code.
class A
{
public:
static int x;
};
int main()
{
A::x = 0;
}
if you compile and link the code, the linker will report error, say " can
not resolve A::x ". why?
c++ static member is special. where declared in the class body, it just like
the " extern A::x ", it's just a name here and don't alloc any memory. So,
when link, the linker cannot find the real A:x. We can translate the C++ code
to the following C code.
extern int A_x;
int main()
{
A_x = 0;
}
Ofcourse, linker cannot find A_x.
So, wo must alloc memory for x.
class A
{
public:
static int x;
};
int A:x; // the really A::x is here.
int main()
{
A::x = 0;
}
Now, everything is OK.
class A
{
public:
static int x;
};
int main()
{
A::x = 0;
}
if you compile and link the code, the linker will report error, say " can
not resolve A::x ". why?
c++ static member is special. where declared in the class body, it just like
the " extern A::x ", it's just a name here and don't alloc any memory. So,
when link, the linker cannot find the real A:x. We can translate the C++ code
to the following C code.
extern int A_x;
int main()
{
A_x = 0;
}
Ofcourse, linker cannot find A_x.
So, wo must alloc memory for x.
class A
{
public:
static int x;
};
int A:x; // the really A::x is here.
int main()
{
A::x = 0;
}
Now, everything is OK.