Does constant static member variables of a class or a struct in C++ need not be defined separately?
Is this correct?
struct test
{
const static int x;
};
int test::x;
Does constant static member variables of a class or a struct in C++ need not be defined separately?
Is this correct?
struct test
{
const static int x;
};
int test::x;
No that's not correct. The definition must match the declaration and x
is const int
, not int
. As a const
variable of POD type it also needs to be initialized. E.g.
const int test::x = 0;
As a const static
member of integral type, it is also allowed to supply the initializer in the definition of the class instead.