Use const to limit the member functions of the class

Source: Internet
Author: User

This article is worth reading carefully. However, I think the above section "const limitation class member functions" is relatively simple, especially the text after "NOTE" is even more impressive, therefore, I would like to provide some additional instructions for this part.

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, but data members cannot be changed. Without the const-modified member functions, data members can be read and written.

In addition, what are the benefits of adding const to the member functions of the class? As the author tells us, "ability to get: constant objects can be operated" is actually a constant (I .e. const) object that 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.

For a const member function, "the data member of the class cannot be modified, and other functions other than the const function cannot be called in the function" is determined by the attributes of the const function, which is completely correct by the landlord.

Please refer to the following complete example and I will explain it again.
 
# 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 );
 
}
Copy code


A Class of Student is designed. The data members include name and score, two constructor functions are available, and one is set Member Data function set_student (). Each has a function get_name () that obtains name and score () and get_score (). Note that both get_name () and get_score () are followed by const, and set_student () is not followed by (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
 
Student stu ("Wang", 85 );
 
Stu. get_name () = "Li ";
Copy code

That is, the name is changed from the original "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
 
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;
 
}
Copy code

Because the student parameter represents a reference to a const Student object, student cannot call non-const member functions such as set_student (). If the get_name () and get_score () member functions also become non-const types, student. get_name () and student. the use of get_score () is invalid, which makes it difficult 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.

This article is from "fqzone"

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.