In C ++, what does const mean after a function?

Source: Internet
Author: User

Q: What does const in C ++: void display () const mean?
Answer:
This means that except for the mutable member variables
Other member variables of this class cannot be modified in this function.

Details:
Const indicates that the function can only be read-only and the value of private variables cannot be modified. Enhance security.
Add const to the implicit this pointer, indicating that this points to const, that is, the data member cannot be changed in this function. Const is a guarantee that this Member will not change the object state.

The const is added to the member function of the class, indicating that this function will not make any changes to the data member of the Class Object (accurately non-static data member.

When designing a class, a principle is that const must be added to all member functions that do not change data members. Const cannot be added to member functions that change data members. Therefore, the const keyword makes more explicit limits on the behavior of member functions: A member function with const modification (that is, the const is placed behind the function parameter table, rather than before or within the function table ), only data members can be read, and data members cannot be changed; no
Const-modified member functions are readable and writable for data members.

In addition, what are the benefits of adding const to the member functions of the class? That is, a constant (that is, a const) object can call the const member function, rather than a non-const modified function. Just as data of non-const type can assign values to the variable of const type, otherwise, it is not true.

Please refer to the following complete example and I will explain it again.

Code: [Copy
To clipboard] # 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 student class is set. The data members include name and score, two constructor functions, and one is set Member Data function set_student (), each function provides the get_name () and get_score () functions for obtaining names and scores (). Please note that get_name () and get_score () are followed
Const, but set_student () is not followed (nor can there be const ).

First, let's talk a little bit about the problem. Why is const added before get_name. If there are no two const, get_name () returns a reference to the name of the private data member. Therefore, the value of the Private member name can be changed through this reference, as shown in

Code: [Copy
To clipboard] student Stu ("Wang", 85 );
Stu. get_name () = "Li"; that is, the name is changed from "Wang" to "Li", which is not what we want to happen. Therefore, add const before get_name () to avoid this situation.

Therefore, the member functions of const should be added after get_name () and get_score (). Can it be modified without const? The answer is yes! However, the cost is that the const object cannot call these two non-const member functions. For example

Code: [Copy
To clipboard] const string & get_name (); // both functions should be set to the 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 next calls are incorrect.
Cout <student. get_score () <Endl;
} The student parameter indicates a reference to a const student object. Therefore, student cannot call non-const member functions such as set_student (). If the get_name () and get_score () member functions become non-const types, student. get_name () and student. get_score ()
Is invalid, which will cause difficulties for us to solve the problem.

Therefore, we have no reason to oppose the use of Const. When adding const, we should add const, so that apart from non-const objects, the const object can also call it.

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.