Const and non-const member functions overload and precautions

Source: Internet
Author: User

Overload of const and non-const member functions of the class

Let's start with an example. Let's look at the string class in the previous article. We provide it with a subscript operator ([]) to read and write characters (char) at the specified position ).

As long as you know the syntax of the C ++ operator overload, You can soon write the following [] OPERATOR overload function:

Char & operator [] (INT posion) // function_1

{

Return data [posion];

};

Note that the return value of this function is a reference. Otherwise, statements such as STR [0] = 'C' are invalid because STR [0] is a left value.

So, is it enough to provide such a function_1? See the following code:

Const string STR = "she ";

Char c = STR [0]; // error! Compilation prompt: Error c2678: Binary "[": the operator that accepts the left operand of the "const string" type is not found (or there is no acceptable conversion)

Obviously, we must also provide a const version of opeartor [] for the const string. As follows:

Char & operator [] (INT posion) const

{

Return data [posion];

}

In this way, when a const String object uses the [] operator, the overloaded version of the const is called.

But, is that OK? Although the above Code is okay, there is a hidden trap. See the following code:

Const string STR = "she ";

STR [0] = 'T ';

The above code can be compiled and run successfully. STR is changed to ""! STR declared as const !!

Now you should know that the return value of the const operator's overload function should also be const. Otherwise, the const object Modification Vulnerability may occur. Modify as follows:

Const char & operator [] (INT posion) const

{

Return data [posion];

}

Okay, now there is no problem!

Let's look back at it. To provide a [] Operator for string to read and write characters at the specified position, we need to provide the following two functions, to provide support for non-const string objects and const string objects respectively:

Char & operator [] (INT posion)

{

Return data [posion];

};

Const char & operator [] (INT posion) const

{

Return data [posion];

}

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.