1. Static data members are declared in the class declaration and initialized in the file containing the class method. Initializes the class that the static member belongs to by using the scope operator. However, if the static member is a const-shaped or enumerated const, you can initialize it in the class declaration.
Declaration and initialization of C + + primer plus p426-p427 class static members
Strnbad.h
Class Stringbad
{
Private
static int num_strings;
...
};
Strnbad.cpp
int stringbad::num_strings = 0;
Static member variables cannot be initialized in a class declaration because the declaration describes how to allocate memory, but does not allocate memory. Note that the initialization statement indicates the type and uses the scope operator, but does not use the keyword static. Initialization is done in the method file, not in the class declaration file, and if the header file is included in several other files in the head file, multiple initialization copies appear, causing an error.
Static data member initialization problem for a class