Regular objects
A constant object must specify that the object is a constant object when defining the object.
The data member in a constant object is a constant variable and must have an initial value, such as
Copy Code code as follows:
Time Const T1 (12,34,36); Define T1 as a constant object
In this case, the value of all data members in the object T1 cannot be modified on all occasions. Objects that want to ensure that data members are not changed can be declared as constant objects.
The general form for defining a constant object is
Class Name Const object Name (argument list);
You can also write the const on the left.
Const Class Name Object name (argument list);
Equivalence
If an object is declared as a regular object, you cannot call the non-const member function of the object (except for implicit constructors and destructors that are automatically invoked by the system).
Or you'll get an error.
This is done in order to modify the values of the data members in the constant object by the member function of the const type, because a const member function is not able to modify the value of the data member in the object (which is also mentioned later).
So how do you refer to the data members in a constant variable? Simply, we just need to declare the member function as a const member function (a constant member function).
Copy Code code as follows:
A regular member function can access data members in a constant object, but still does not allow you to modify the values of the data members in the constant object.
Sometimes there is a requirement in programming that you modify the value of a data member in a regular object member (for example, there is a variable count in a class that should not change its value).
Declare the data member as mutable, such as
Copy Code code as follows:
mutable int count;//defines a data member that can be changed in a constant object
Declare count as a mutable data member, so that you can modify its value with a member function declared as Const.
========================= A simple example program 1.1====================================
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class Student
{
<span style= "White-space:pre" > </span>public:
<span style= "White-space:pre" > </span>student (int n,float s): num (n), score (s) {}
<span style= "White-space:pre" > </span>void change (int n,float s) const{num=n;score=s;}
<span style= "White-space:pre" > </span>void display () const{cout<<num<< "\ T" <<score <<endl;}
<span style= "White-space:pre" > </span>private:
<span style= "White-space:pre" > </span>mutable int num;
<span style= "White-space:pre" > </span>mutable float score;
} ;
int main ()
{
<span style= "White-space:pre" > </span>student const Stud (101,78.5);
<span style= "White-space:pre" > </span>stud.display ();
<span style= "White-space:pre" > </span>stud.change (101,80.5);
<span style= "White-space:pre" > </span>stud.display ();
<span style= "White-space:pre" > </span>return 0;
};
We defined the constant object stud, then called two normal display () and change (), but in the changes function to modify the values of NUM and score in the normal object, so we defined num and score as mutable.
Regular Object Members
1. Regular data members
Its function and usage are similar to those of general variables, with the keyword const to declare the constant data member. The value of a constant data member cannot be changed.
You can initialize a constant data member only through the constructor's parameter initialization table.
Declaring a constant data member in a class body
Copy Code code as follows:
const int num;//declaration Hour as constant data member
Defining constructors outside of a class
Copy Code code as follows:
student::student (int n,float s): num (n), score (s) {} through Parameter initialization table for constant data member Num and score initialization
When a data member is declared in a class body as a constant data member, the value of that data member in all objects of the class is immutable, but the value of the variable member in the different objects can be different (specified separately on initialization).
2. Regular member function
If you declare a member function as a constant member function, you can reference only the data members in this class, not the members.
Note: A regular object can only refer to a regular member function
Regular member functions are defined in the form of:
Copy Code code as follows:
void Print () const;//Note that the position of the const is after the function name and parentheses
A const is a part of a function type that has a const keyword in both declaring and defining functions, and does not need to be const when invoked.
Regular member functions can either const data members or refer to non-const data members, but they cannot be modified;
Functions that are not regular member functions can call Const data members, but cannot modify them or call non-const data members, and can modify them.
The specific situation, as shown in Figure 1:
Also pay attention to three points:
1. Do not mistake the member function in the normal object as a constant member function, and the constant object only guarantees that the values of all its data members are not modified.
2. If the member function in the normal object is not declared with a const declaration, the compilation system will treat it as the most non-const member function.
3. Also note that a regular member function cannot invoke another COSNT member function.