C + + member functions before and after adding const modifier differences

Source: Internet
Author: User

Blog reprinted from: https://www.iteblog.com/archives/214.html

Analyze the following procedure to illustrate the function of the member function suffix const and the const before the member function

#include <iostream>using namespacestd;classTestClass { Public: size_t Length ()Const; Const Char*getpcontent (); voidSetlengthvalid (BOOLislengthvalid);Private:    Char*pcontent;       size_t ContentLength; //A    BOOLLengthisvalid;//Bsize_t precontentlength;}; size_t testclass::length ()Const{//Add const after function name    if(!lengthisvalid) {ContentLength= strlen (pcontent);//CLengthisvalid =true;//D    }     returnContentLength;} Const Char* Testclass::getpcontent () {//Add const before the function name    returnpcontent;} voidTestclass::setlengthvalid (BOOLislengthvalid) {Lengthisvalid=Islengthvalid;} intMainvoid) {TestClass*TC =NewTestClass; TC->setlengthvalid (false); TC-length (); Char* content = tc->getpcontent ();//E    return 0;}

The length function and the Getpcontent function in the class TestClass add the const modifier after the function name, respectively, and if you attempt to compile the above code, you will get the following error:

There are three errors, three places in code C, D, and E. Why the code at C and D goes wrong for the following reasons :
The length function name is appended with the Const modifier, which means that the member object of the function is not allowed to be modified. As we all know, in the member function of the class, the default is the this pointer in the first position of the member function, if in the member function (only the member function, if the static function of the class or is a non-member function can not be added after the function name const) after const, Indicates that the value of this pointer is not modifiable and can only be read. The length function above may modify the value of the ContentLength and lengthisvalid inside, so the compiler is definitely not allowed, so there is an error.
The workaround is to precede the members of A and b at the class with the mutable modifier:

bool lengthisvalid;

From the literal meaning, Mutalbe is "mutable, variable," and constant (both const in C + +) are antonyms. In C + +, mutable is also set to break the limits of Const. Variables that are modified by mutable will always be in a mutable state, even in a const function. This will not be an error at C and D.
So why is there an error at E. This is because the const modifier is added before the function name Getpcontent, which means that the value returned by it can only be read and cannot be modified. and the content of E is a char * can be modified, which is the opposite of the const, so there is an error. The workaround is to precede char * with the Const modifier, which is:

Const Char // E

Then go to compile and run, so there is no error.

C + + member functions before and after adding const modifier differences

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.