Add const--c++ const member function after function

Source: Internet
Author: User

the class's member function is followed by a const, which indicates that the function does not make any changes to the data members of the class object (accurately, non-static data members) .

When designing a class, one principle is to add a const to the member function that does not change the data member, and the member function that changes the data member cannot be const. So 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 the data member can be read, the data member cannot be changed, the member function without the const modifier, The data member is readable and writable.

Besides, what is the benefit of adding a const after the member function of a class? The landlord told us: " get the ability: You can manipulate the constant object ", in fact, it should be a constant (that is, const) object can call the const member function, but not the non-const decorated function. Just as non-const types of data can assign values to variables of the const type, the reverse is not true.

for const member functions, "cannot modify the data members of a class, cannot invoke other functions that are not const in the function", which is determined by the Const property, the landlord is absolutely correct.

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 theconst 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.

Add const--c++ const member function after (GO) function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.