Clause 21: Use const whenever possible

Source: Internet
Author: User

For pointers, you can specify the pointer itself as the const, or specify the data referred to by the pointer as the const, or both as the const, and neither of them as the const:

Char * P = "hello"; // non-const pointer, // non-const data const char * P = "hello"; // non-const pointer, // const data char * const P = "hello"; // const pointer, // non-const data const char * const P = "hello"; // const pointer, // const data

Some of the powerful functions of const are based on its application in function declaration. In a function declaration, const can refer to the return value of a function or a parameter. For a member function, it can also refer to the entire function.

 

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? That is, a constant (that is, a const) object can call the const member function, rather than a non-const modified function. A non-const object can call a const or non-const modifier function at will. Just as data of non-const type can assign values to the variable of const type, otherwise, it is not true.

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 ;}; void output (const student & Stu) {// cout is called normally <Stu. get_name (); cout <Stu. get_score ();}

If the get_name and get_score functions are not modified by const, the const student variable cannot call these two functions.

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 () {return name;} int get_score () {return score;} PRIVATE: string name; int score;}; void output (const student & Stu) {// cout compilation error <Stu. get_name (); cout <Stu. get_score ();}

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";

 

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.