(1) Why do I need class static members?
Sometimes multiple objects of a class need to access a global object, in which case "providing a global object" is more efficient than "Maintaining a separate data member for each class". Static members of a class have two more benefits than global variables: (a) There is no possibility of conflict with other global names in the program, and (b) can be set to private to realize information hiding.
(2) Characteristics of class static members
For non-static data members, each class object has its own copy, while static data members have only one copy of each class type. Because static data members are assigned to the global data area and therefore must exist when the program starts running, the spatial allocation and initialization of static data members cannot be done in the main function or other functions (and static data members are not allowed in the local class). As a result, the spatial allocation and initialization of static data members can only be in the following three ways: (a) The header file of the class: Here are the declarations of the classes. But there is an unavoidable problem: for static data members, there can be only one definition in the program, and the header file of the class may be repeatedly referenced and duplicate defined; (b) The global data Declaration and definition at the front of the main function: This method is also problematic, Each program that uses the class must define the static members of the class here, which is unrealistic; (c) The internal implementation of the class definition: This is the ideal way to include only the header file when referencing.
#include "account.h"
double Account::_interestRate = 0.05
(3) Definition of static member of class
There are several considerations when defining static data members: (a) when defined outside a class definition, the name of a static member must be qualified by its class name, before it can be added static; (b) static data members of an ordered type such as int can be initialized in the class definition, but still need to be defined outside the class definition. However, the initial value cannot be specified at this time;
// 头文件
class Account
{
// ...
private:
static const int nameSize = 16;
static const char name[nameSize];
};
// 文本文件
const int Account::nameSize; // 必需的成员定义,此处不可再进行初始化
const char Account::name[nameSize] = "Saving Account";
(4) Access to static members of a class:
Static data members of the class can be accessed directly in a member function of a class, and access can be restricted by using a member access operator or a class name in a non-member function.
(5) Static member function:
Static member functions cannot access non-static data members belonging to a specific class object, nor can they access non-static member functions, he can only access static data members and invoke the rest of the static member functions. This prevents the Non-static data members of the class from being tampered with. Static member functions do not have this pointer, and any explicit or implicit reference to this pointer in a static member function will result in a compile-time error.