I have the following situation:
class Foo
{
public:
static const Foo memberOfFoo;
........
}
So the thing is I can’t initialize it in the same line where I declared it, and, I can’t initialize it via Initializations List in the constructor, does anyone know what to do?
Put this outside of the class definition then:
That is the definition of
Foo::memberOfFoo, which can supply an initializer and has to go into the.cppfile (like any other definition of objects, it can only appear once in the whole program, otherwise you will get linker errors).Sometimes you will find code that doesn’t have definitions for its static data members:
Omitting the definition like that is valid only if
A::xis never address-taken and never passed to reference parameters. A more formal way to say when it is valid to omit the definition is: “When all uses of A::x immediately read the stored value of A::x”. That’s the case for many static integer constants.