One, static member: Static decoration, each class has only one copy, for all of the objects of that class are shared.
1. Static data members:
In-class declarations, out-of-class initialization. As with global variables, allocate memory in the data segment.
eg
Class a{
static int co;
};
int a::co=0;
2. Static member functions:
1). Static member functions are specifically used to access static members of a class, cannot access non-static members, and does not have the this pointer. Declared by static within a class, can be defined outside the class, and no static is required as a prefix at this time.
eg
Class a{
static int co;
static int Getco () {return CO;}
}
2). Static member functions can be called directly through the class name or through an object. The format is: Class Name:: Static member function, object. static member function.
Two, constant object and constant member: const
1. Constant object: Must be initialized by the constructor at the time of definition, only the regular member function can be called, and can only be accessed by the regular member function, the lifetime is not allowed to be modified.
Definition Format: Type const object name, or const type object name.
2. Constant data member: must be initialized in the initialization list of the constructor, not allowed to be modified during lifetime.
Definition Format: Type const data member name, or const type data member name.
3. Constant member function: Cannot update the object's data members (but can be accessed), cannot call the class of the very member functions, in the function declaration and definition must be added to the const keyword.
Definition format: Returns the type member function name (parameter table) Const.
eg
Class a{
int x;
int day () const;
}
int A::d ay () const
{
return x;
}
In addition, const can be used for function overloading.
Eg:void print () and void print () const are two functions that can be overloaded.
Static members of a class, as well as constant objects, constant members