The following article focuses on C ++ static members. static data members of the class should be displayed in the Code for initialization and must be performed outside the class, the differences between C ++ static members and common data members are as follows:
1. static data member
The method for defining a C ++ static member in a class is to add the keyword static before the member.
The statement format for defining static data members is as follows:
- Class Name
-
- {
-
- ......
-
- Member name of the static type specifier;
-
- ......
-
- };
C ++ static members are shared by all objects in the class. The space occupied by static data members is not allocated with the generation of objects, nor is it recycled with the disappearance of objects. The operations on static data members are different from the operations on general data members in the class. static data members defined as private members cannot be accessed by the outside world. Static data members can be accessed by functions with any access permission.
Static data members share all objects of the class, but do not belong to any specific object. Therefore, static data members of the class must be initialized, however, its initialization cannot be performed in the class constructor. Its initialization statement should be written in the global area of the program, and its data type and class name must be specified, the initialization format is as follows:
Type Class Name: variable name = value. For static data members described in the public part of the class, they can be directly accessed outside the class without using member functions, however, you must use the class name to specify the class to which it belongs. The ACCESS format is: for static data members that are described in the non-public part of the class, they can only be accessed by member functions of the class, the access method is the same as that of common data members in the category class, but cannot be accessed outside the class.
2. static member functions
The definition of a static member function is the same as that of a common member function, but the static keyword is prefixed. The definition format is as follows:
- Class Name
-
- {
-
- ...
-
- Static function name and parameter)
-
- {Function body}
-
- ...
-
- };
Note:
1) Class C ++ static member functions can only member static data of the category, but cannot member common functions in the category of non-static data ), because normal data members make sense only when class objects exist.
2) C ++ static member functions are associated with the class instead of the class objects. Therefore, public static member functions in the class's external call class, you must add "Class Name:" To the left of the class, instead of calling the public static member function through the object name. Private Static member functions in the class cannot be called outside the class.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code