Static and const are very important concepts in C + + programming, and the examples in this article enumerate the rules and usages of static and const in C + + classes. For reference. The details are as follows:
First, use the code to illustrate. The sample code is as follows:
Class A
{public
:
A (): M (//const) member must initialize
{
q = n
in the constructor's initialization list) void Fun1 () const
{
m++; Error. A const member is a constant and cannot change its value.
n++; That's right. The static variable n belongs to the class, but each object's function can access and change it.
q++; Error. A const member function cannot change the value of a data member.
}
static void Fun2 ()
{
m++; Error. A const member is a constant and cannot change its value.
n++; That's right. A static member function can access and change the value of a static variable.
q++; Error. A static member function cannot access and change the value of a non-static data member.
}
const int m;
static int n;
static const int p;
int q;
};
int a::n = 5; The static member must be initialized outside of the class without the keyword static, but to specify class scope a::
const int A::P =; The static const member is initialized outside the class like a static member (rather than in the constructor initialization list), and remember to add the keyword const
The following is a detailed description.
One, static key word
1.static data member
A static data member is a class, is not part of any specific object, and does not occupy the memory space of the object. can be accessed in the form of a::n, or through object access (although not a specific object, but all objects are common).
The initialization of the static data member must be initialized outside the class, with int a::n = 5; In this form, remember to indicate the type and the class to which it belongs, without adding the keyword static.
2.static member function
A static member function can only access static data members or static member functions and cannot access non-static data members and non-static member functions.
Two, the const keyword
1.const data member
Must be initialized in the constructor initialization list. The reason can be understood to have the following two points.
(1) A member of a class cannot initialize a declaration, such as int c = 3 in a class body;
(2) You cannot assign a value in a member function because the const cannot be changed.
2.const member function
The const member function can access all data members but not the value of any one of the data members of the object, but can change the value of the static member (the static member belongs to the class and not to the specific object)
3.const objects
A const object can only invoke a const member function and can only change a static member.
Three, static const keyword
The first thing to remember is that the static const representation is both static and const, both of which are characteristic.
The static const int p;//and the const static int p;
Initialize like a static member, outside of a class, but with a const.
I hope this article is helpful to the object-oriented programming of C + +.