The C ++ programming language has been around for 20 years since its appearance. In the past 20 years, it has played a very important role in the development field by virtue of its unique performance and flexible application methods. Today, we will analyze the characteristics of this language from the C ++ static data member.
- Analysis of basic implementation methods of C ++ Using Interfaces
- Analysis of the similarities and differences between the left and right values of C ++
- C ++ template parameters
- C ++ template function reloads different comments
- Interpretation of c ++ pointer-heavy application code
When the static keyword is added before the declaration of the data member in the class body, the data member becomes the static data member of the class. Like other data members, static data members also comply with public/protected/private access rules. Static data members also have the following features:
1. Definition of C ++ static data members.
Static data members are actually global variables in the category. Therefore, the definition (initialization) of static data members should not be put in the header file.
The definition is the same as that of global variables. Example:
Xxx. h file
- Class base {
- Private:
- Static const int _ I; // declaration, standard c ++ supports sequential type initialization in the class body, but vc6 does not.
- };
Xxx. cpp File
- Const int base: _ I = 10; // when defining (initialization), private and protected access is not restricted.
Note: do not try to define (initialize) static data members in the header file. In most cases, this will lead to repeated definitions of such errors. Not even if # ifndef # define # endif or # pragma once is added.
2. The C ++ static data member is shared by all objects of the class, including objects of the class derived class. That is, the derived class Object shares the static data member of the base class with the base class object. Example:
- Class base {
- Public:
- Static int _ num; // Declaration
- };
- Int base: _ num = 0; // real definition of static data members
- Class derived: public base {
- };
- Main ()
- {
- Base;
- Derived B;
- A. _ num ++;
- Cout <"base class static data number _ num is" <a. _ num <endl;
- B. _ num ++;
- Cout <"derived class static data number _ num is" <B. _ num <endl;
- }
- // The result is 1 or 2. The derived class and the base class share a static data member.
3. The C ++ static data member can be an optional parameter of the member function, but not a common data member. Example:
- Class base {
- Public:
- Static int _ staticVar;
- Int _ var;
- Void foo1 (int I = _ staticVar); // correct. _ staticVar is a static data member.
- Void foo2 (int I = _ var); // error. _ var is a common data member.
- };
4. the type of the C ++ static data member can be the type of the class to which it belongs, but the type of the common data member cannot. A common data member can only be declared as a pointer or reference of the class type. Example:
- Class base {
- Public:
- Static base _ object1; // correct, static data member
- Base _ object2; // Error
- Base * pObject; // correct, pointer
- Base & mObject; // correct, reference
- };
5. I don't know whether it is a feature in Standard c ++ or a feature of vc6. The value of the C ++ static data member can be legally changed in the const member function. Example:
- Class base {
- Public:
- Base () {_ I = 0; _ val = 0 ;}
- Mutable int _ I;
- Static int _ staticVal;
- Int _ val;
- Void test () const {// const member function
- _ I ++; // correct, mutable data member
- _ StaticVal ++; // correct, static data member
- _ Val ++; // Error
-
- }
- };
- Int base: _ staticVal = 0;