Initialization of static members:
As with global objects, a static data member can only provide a definition in a program, which means that the initialization of a static data member should not be placed in the header file but should be placed in a file containing the class's non-inline function definition.
Transferred from: http://blog.csdn.net/veryday_code/article/details/7921833
There is only one member that can be initialized in a class, which is a static constant member.
class a{ private: staticconstint0// Static constant members can be initialized within a class };
Conclusion:
- A static constant data member can be initialized within a class ( that is, initialized at the same time as declared within a class), or it can be initialized in an implementation file outside of a class, which cannot be initialized in a constructor or initialized in a constructor's initialization list;
- Static and very data members can only be initialized in an implementation file outside the class, that is, in the constructor, and cannot be initialized in the initialization list of the constructor function;
- A non-static constant data member cannot be initialized within a class, nor can it be initialized in a constructor, but only and must be initialized in the constructor's initialization list;
- nonstatic non-static data members cannot be initialized within a class, can be initialized in constructors, or can be initialized in a constructor's initialization list;
Static member initialization for classes in C + +