Use and initialization of constant data members
We already know the concept of constant when we learn C language. The keyword is const . The so-called "constant", or a variable modified by "constant" cannot be changed. For example, a variable modified with const becomes Constant variable, this value cannot be changed.
Well, in C++, there is also the concept of constant. The additional difference is that in addition to modifying general variables into constant variables, const can also be used to modify an object and turn it into a constant object. And the data members and member functions that can modify the class are called constant data members and constant member functions of the class respectively.
Next, we will explain constant data members , constant member functions , and constant objects in sequence.
1. Constant data members:
The usage of constant data members is the same as our usage in C language, except that this part of the data appears in the class, and the format used is as follows:
Data type const data member name;
or
const data type data member name;
Members modified by const must be initialized and cannot be changed, and the initialization method is performed in the initialization list of the constructor of the class.
In addition, there is a special case, if the member is a static type, that is, a static constant data member , because it is a static attribute, initialization needs to be initialized outside the class. Let's put the situation just mentioned into the code to illustrate:
#include<iostream>usingnamespacestd;classClock{private:constinth;//Modify h as a constant type member constintm;//Modify m as a constant type member intconsts;//Both the above two usages can be used staticconstintx;public:Clock(inta, intb,intc):h(a),m(b),s(c){cout<<Constrctor!Called<<endl;}intShowTime(){cout<<h<<:<<m<<:<< s<<endl;return0;}intGetX(){cout<<x<<endl;return0;}};constintClock::x=99;intmain(){ClockA(12,10,30);A.ShowTime() ;A.GetX();return0;}
Run the demo results:
Please pay attention to the four constant data members in the class. Among them, X is a static type. Because of its special static properties, it needs to be initialized outside the class, even if X is a private type!
2. Constant objects:
In C++, an object can be declared as a const type, that is, a constant object. After this declaration, the object cannot be changed during the entire life cycle, so it must be initialized by the constructor when defining. The definition format is as follows:
Type const object name;
or
const type object name;
It should be noted that constant objects cannot access non-member functions in the class, but can only access constant member functions (Part 3 below). Let's look at an example below:
#include<iostream>usingnamespacestd;classClock{private:constinth;//Modify h to be a constant type member constintm;//Modify m to be a constant type member intconsts;//Both the above two usages are possible intx;public:Clock(inta, intb,intc):h(a),m(b),s(c){x=99;cout<<Constrctor!Called<<endl;}intShowTime(){cout<<h<<:<<m< <:<<s<<endl;return0;}intGetX()const{//x=99;cout<<x<<endl;return0;}};intmain(){constClockA(12,10,30);constClockB (14,20,50);//A=B;//A.ShowTime();A.GetX();return0;}
Pay attention to the statements on lines 39 and 40 in the code. Normal compilation will report an error because A is a constant object and cannot be assigned a value, and the ShowTime function is a non-member function. The 41 lines of code can be executed, and the effect is as follows:
3. Constant member functions:
Similarly, after a member function in a class is modified by const, it becomes a constant member function. The definition of a constant member function is as follows:
Return type function name (parameter list column) const;
Need to pay attention to:
(1) The definition and declaration of constant member functions must contain const;
(2) Constant member functions can only call constant member functions, but cannot call extraordinary member functions, and cannot access but cannot change extraordinary member variables.
For example, the following code:
#include<iostream>usingnamespacestd;classClock{private:constinth;//Modify h to be a constant type member constintm;//Modify m to be a constant type member intconsts;//Both the above two usages are possible intx;public:Clock(inta, intb,intc):h(a),m(b),s(c){x=99;cout<<Constrctor!Called<<endl;}intShowTime(){cout<<h<<:<<m< <:<<s<<endl;return0;}intGetX()const{//x=99;cout<<x<<endl;return0;}};intmain(){constClockA(12,10,30);A .GetX();return0;}
Pay attention to the 29th and 30th lines of code. If you use the 29th line of code, a compilation error will be reported. The actual running effect is as follows:
The establishment of the concept of constants in C++ clarifies the boundaries between change and immutability of each object in the program, and also enhances the safety and controllability of C++ programs.
Please try it out on your own.