<> Note: This article is organized by a post in a forum.
Remember: Static data members are usually declared in the class declaration and initialized in files containing Class methods. during initialization, the scope operator is used to indicate the class to which the static member belongs. however, if the static member is an integer or enumerative const, You can initialize it in the class declaration !!!
# Include < Iostream > Using Namespace STD ; Class Test { Public : Static Int Num ; } ; Int Test : : Num = 0 ; Void Main ( ) { Cout < < Test : : Num < < Endl ; Test : : Num = 20 ; Cout < < Test : : Num < < Endl ; }
|
Generally, static data members are initialized out of the class definition, just as a member function is defined in the class definition.
In this definition, the names of static members must be qualified by their class names.
Int Test::Num=0;
Like a global objectProgramOnly one definition can be provided. This means that the initialization of static data members should not be placed in the header file, but should be placed in a non-inline function definition file containing classes, static data members can be declared as any type. They can be const object arrays or class objects.
# Include < String > Class Account { //... Private : Static Const String Name ; } ; Const String Account : : Name ( "Savings Account" ) ;
|
As a special-case ordered const static data member, it can be initialized with a constant value in the class body, for example, if you decide to use a character array instead of a string to store the account name, you can use the INT-type const data member to specify the length of the array. For example:
// Header file Class Account { //... Private : Static Const Int Namesize = 16 ; // It seems that this is not supported in VC Static Const Char Name [ Namesize ] ; } ; // Text file Const Int Account : : Namesize ; // Required member Definitions Const Char Account : : Name [ Namesize ] = "Savings Account" ;
|
In this special case, there are some interesting things worth noting. The const static data member of the ordered type initialized with a constant value is a constant expression, if you need to use the named value in the class body, the class designer can declare such a static data member, for example, because the const static data member namesize is a constant expression, the class designer can use it to specify the length of the array data member name, when initializing a const static data member in the class body, the member must still be defined outside the class definition.
However, because the initial value of the static data member is specified in the class body, the initial value cannot be specified for definitions other than the class definition, because name is an array that is not an ordered type, it cannot be initialized in the class body. Any attempt to do so will result in a compilation error. For example:
class account { //... private : static const int namesize = 16 ; // OK: ordered type static const char name [ namesize ] = " Savings Account " ; // error } ;
|
name must be initialized outside the class definition. This example also illustrates that the namesize of the Member specifies the length of the array name, and the definition of the array name appears outside the class definition,
const char account: name [namesize] = "Savings Account";
the namesize is not limited by the class name account. although namesize is a private member, the definition of name is still correct. How can this problem be solved? For example, the definition of a similar member function can reference a private member of a class. The definition of a static data member can also reference a static data member, name is defined as a private data member of an account in the domain of its class when the qualified account: Name is viewed.