Some member functions change objects, while some member functions do not change objects.
For example:
Int point: Gety ()
{
Return yval;
}
When this function is called, The point object is not changed, and the following function changes the point object:
Void point: setpt (int x, int y)
{
Xval = X;
Yval = y;
}
To make the meaning of member functions clearer, we can add const to the function prototype of the member functions without changing the object:
Class Point
{
Public:
Int getx () const;
Int Gety () const;
Void setpt (INT, INT );
Void offsetpt (INT, INT );
PRIVATE:
Int xval, yval;
};
Const member functions should be added with the const limitation in both the function prototype description and Function Definition:
Int point: Gety () const
{
Return yval;
}
Class set {
Public:
Set (void) {card = 0 ;}
Bool member (const INT) const;
Void addelem (const INT );
//...
};
Bool set: Member (const int ELEM) const
{
//...
}
A non-constant member function cannot be called by a constant member object because it may attempt to modify the data member of a constant:
Const set S;
S. addelem (10); // invalid: addelem is not a constant member function.
S. Member (10); // correct
Except for this rule, constructor and destructor are never defined as constant members, but can be called by constant objects (automatically called ). They can also assign values to the data members of constants unless the data members are constants themselves.
Why do we need a const member function?
In the member functions defined by the class, some member functions do not change the data members of the class. That is to say, these functions are read-only functions, some functions need to modify the values of class data members. If you add the const keyword to all functions that do not change data members for identification, it can obviously improve the readability of the program. In fact, it can also improve the program's reliability. It has been defined as a const member function. Once it attempts to modify the value of the data member, the compiler will handle it by mistake.
Const member functions and const objects
In fact, the const member function has another function, that is, constant object correlation. For built-in data types, we can define their constants, and user-defined classes can also define their constant objects. For example, the method for defining an integer constant is:
Const int I = 1;
You can also define a constant object. Assume that there is a class classa, and the method for defining the constant object of this class is:
Const classa A (2 );
Here, a is a const object of the classa class, And the constructor parameter passed to it by "2. The data member of the const object cannot be changed during the life cycle of the object. However, how can we ensure that the data members of this class are not changed?
To ensure that the data member of the const object is not changed, in C ++, the const object can only call the const member function. If a member function does not actually modify the data member in any form, but it is not limited by the const keyword, it cannot be called by a constant object. Here is an example to illustrate this problem:
Class C
{
Int X;
Public:
Int getx ()
{
Return X;
}
Void setx (int x)
{
This-> X = X;
}
};
Void main ()
{
Const C constc;
Cout <constc. getx ();
}
If we compile the above program code, the compiler will encounter an error message: constc is a constant object, which can only call the const member function. Although the getx () function does not actually change the data member X, it cannot be called by the constc object because there is no const keyword restriction. If we use the above bold code:
Int getx ()
Rewrite:
Int getx () const
Then re-compile the program.
Use of const member functions
The const member function indicates that the member function can only read class data members, but cannot modify class member data. When defining a const member function, place the const keyword between the parameter table of the function and the function body. Some may ask: why not place const before function declaration? This means that the return value of the function is a constant and the meaning is completely different. The following is an instance that defines the const member function:
Class X
{
Int I;
Public:
Int F () const;
};
The const keyword must be repeated in function implementation in the same way; otherwise, the compiler will regard it as a different function:
Int X: F () const
{
Return I;
}
If F () tries to change I in any way or call another non-const member function, the compiler will give an error message. Any function that does not modify member data should be declared as a const function, which helps improve the readability and reliability of the program.
Object. member functions
Object member functions
1. Const const pair
2. Const non-const Error
3. Non-const pair
4. Not-const non-const pair
Member functions call member functions
Member function
5. Const const pair
6. Const non-const Error
7. Non-const pair
8. Non-const non-const pair