Static member initialization of C++ classes

  • 2020-04-02 01:43:45
  • OfStack

Remember: Static data members are usually declared in class declarations and initialized in the file that contains the class methods.


#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;
}

Typically static data members are initialized outside of the class definition just as a member function is defined outside of the class definition, where the name of the static member must be qualified by its class name, as in the example above
Int test: : num = 0;
Like the global object for static data members in the program can only provide a definition, this means that the static data members of initialization should not be placed in a header file and should be in the class of the inline function definitions file, static data members can be declared as they can be any type array of const object or class objects and so on

#include <string>
class Account {
// ...
private:
static const string name;
};
const string Account::name("Savings Account");

As a special case, the ordered const static data member can be initialized with a constant value in the class body. For example, if we decide to use an array of characters instead of a string to store the name of an account, we can use an int const data member to specify the length of the array, for example:

//The header file
class Account {
// ...
private:
static const int nameSize = 16;//It seems that vc does not support this
static const char name[nameSize];
};
//Text file
cons tint Account::nameSize;//Required member definitions
const char Account::name[nameSize]="Savings Account";

In this special case there is something interesting to note that with a constant value as the initialization sequence type const static data member is a constant expression is constant expression, if you need to use this in the class body named value, then the class designer can declare such static data members, for example because of const static data members nameSize is a constant expression so the class designer can use it to specify the length of the array data member name, When a const static data member is initialized in a class it must still be defined outside of the class definition
However, since the initial value of this static data member is specified in the class body, the definition outside the class definition cannot specify the initial value. Because the name is an array and not an ordered type, it cannot be initialized in the class body.

class Account {
// ...
private:
static const int nameSize = 16;//Ok: ordered type
static const char name[nameSize]= "Savings Account";//error
};

The name must be initialized outside of the class definition. This example also illustrates that the member nameSize specifies the length of the array name, and the definition of the array name appears outside of the class definition,
Const char Account::name[nameSize] = "Savings Account";

The nameSize is not modified by the class name Account qualification. Although the nameSize is a private member, the name definition is correct. Just as the definition of the same kind of member function can refer to the private member of the class, the definition of the static data member can also refer to the static data member. The definition of the name is in the field of its class. When the qualified modifier Account::name is seen, it can refer to the private data member of the Account.


Related articles: