The tangle of C + + Const member functions

Source: Internet
Author: User
Tags bitwise

The beauty of Const is that it allows you to specify a semantic constant. You should use it as much as possible in your code, global scope, namespace, inside the class, whether it's static or non-static, and you can use it.

A const object that can only call the const function. This object may be modified because it is not a const function. Therefore, if a member function does not modify any non-static member data, you should declare it as Const. Like what:

class String {     public:       char& operator[](int position)       { return data[position]; }       const char& operator[](int position) const       { return data[position]; }??private:  string *data;};

However, the world days wondering people to wish. Consider the code:

class String {     public:        char& operator[](int position) const       { return data[position]; }??private:  char *data;};const String BillGates = "employer";BillGates[7] = ‘e‘;cout << BillGates << endl;

The result is that billgates from employer into employee, which is not what we expected. A const object that calls a const function, and the result modifies the object, which you think is ridiculous. C + + also feels aggrieved because the function does not modify member data and conforms to the const member function rules. It is so smooth through the C + + compiler's security screening, without any alarm, seemingly calm, but actually hidden in the murderous. In that case, is there no solution? No, you just need to change char& to const char&.

The problem is solved, but involves a problem, that is how to see the Const. Words, a moment of controversy, the formation of two mainstream: the bitwise faction and the logic faction. Bitwise that const should not be able to modify object entities, while logic believes that real implementations can have modified object entities as long as the client does not see the changes. C + + chose the bitwise faction, after all, it is much easier to realize. The logic faction is very dissatisfied, give the following example, the code is as follows:

class String {public:    size_t length() const;private:    char *data;    size_t dataLength;    bool lengthIsValid;};size_t String::length() const{    if (!lengthIsValid) {        dataLength = strlen(data);      // error!        lengthIsValid = true;           // error!    }   return dataLength;}

Length () is simply a string, declaring it to be const, and it makes sense to buffer length data in order to respond efficiently to frequent queries. However, with this harmless code, the C + + compiler does not let it pass.

Ah, this really makes C + + difficult! Whether you can modify an object or not, this is a problem, it is a tangled problem. If you choose logic, the const is a dummy. Perhaps Bjarne Stroustrup should learn Dennis Ritchie, and believe the code written by the programmer. Bjarne Stroustrup know that, because of this, countless programmers have suffered from the C language torture. He can't do that without any mistakes. But what does logic say is not unreasonable? Bjarne Stroustrup in the room, he couldn't make up his mind.

Mutable out. C + + decided to stick to the bitwise faction, while providing mutable to enable logic to fulfill their aspirations. Thus, the above code can be written like this:

class String {public:    size_t length() const;private:    char *data;    mutable size_t dataLength;    mutable bool lengthIsValid;};size_t String::length() const{    if (!lengthIsValid) {        dataLength = strlen(data);      // ok        lengthIsValid = true;           // ok    }   return dataLength;}

Finally, the code passes through the compiler's security check, and the C + + compiler is happy to produce the target instruction.

Reference: "Ffective C + +"

The tangle of C + + Const member functions

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.