Class is followed by a const, indicating that the function does not have a data member for the class object
(accurately, non-static data members) for any change.
When designing a class, one principle is to add a const to the member function that does not change the data member.
For member functions that change data members, you cannot add Const. Therefore, the Const keyword defines the behavior of the member function more explicitly:
A const-Modified member function (that is, the const is placed after the function parameter table, not in front of the function or in the parameter table).
Only data members can be read, data members cannot be changed, member functions without const modifiers, data members are readable and writable.
For const member functions, "You cannot modify a data member of a class, you cannot call other functions that are not const in a function."
This is determined by the properties of the const,
Take a look at the complete example below, and then I'll make a few more notes.
#include <iostream>;
#include <string>;
using namespace Std;
Class Student {
Public
Student () {}
Student (const string& nm, int sc = 0)
: Name (nm), score (SC) {}
void set_student (const string& nm, int sc = 0)
{
name = NM;
Score = SC;
}
Const string& get_name () const
{
return name;
}
int Get_score () const
{
return score;
}
Private
String name;
int score;
};
Output student ' s name and score
void Output_student (const student& Student)
{
cout << student.get_name () << "\ T";
cout << student.get_score () << Endl;
}
int main ()
{
Student Stu ("Wang", 85);
Output_student (Stu);
}
A class Student is designed, the data member has name and score, there are two constructors, there is a set member data function Set_student (), each has a function to get name and score Get_name () and Get_score (). Note that get_name () and Get_score () are all followed by a const, and set_student () does not follow (and cannot have a const).
First, let's say a little off-topic, why Get_name () is also added in front of the const. If there is no front and back two const, get_name () returns a reference to the private data member name, so the reference can change the value of the private member name, as
Student Stu ("Wang", 85);
Stu.get_name () = "Li";
That is, the name from the original "Wang" into "Li", and this is not what we want to happen. Therefore, the get_name () is preceded by a const to avoid the occurrence of this condition.
So, Get_name () and Get_score () are two of the member functions that should be added to the const, if not the const modifier? The answer is YES! However, the cost of doing this is that the Const object will no longer be able to invoke these two non-const member functions. Such as
Const string& get_name (); Both of these functions should be set to a const type
int Get_score ();
void Output_student (const student& Student)
{
cout << student.get_name () << "\ T"; If Get_name () and Get_score () are non-const member functions, this and the next call are wrong
cout << student.get_score () << Endl;
}
Because the parameter student represents a reference to a const student type object, student cannot call a non-const member function such as Set_student (). If the get_name () and Get_score () member functions also become non-const, then the use of the above Student.get_name () and Student.get_score () is illegal, which can make it difficult for us to deal with the problem.
Therefore, there is no reason to oppose the use of const, which should be const when added, so that a const object can invoke a member function in addition to a non-const object.
Usage of const in C + +