C + + member functions const overloading

Source: Internet
Author: User

It is well known that C + + classes implement function overloading in two cases: the type of the function parameter is different, the number of parameters is different. It has nothing to do with the return value of the function. There is also a class member function overload, which does not require a different parameter type for the function. Here's an example:

#include <iostream>using namespace Std;class a{public:    A (int a): _a (a) {}    void print () const// The data member object cannot be modified in the function body, otherwise the error    {        cout << const << _a << Endl;    }    void print ()//function body can modify member object    {        _a =;        cout << "Non-const" << _a << Endl;    } Private:    int _a;}; int main () {    a A (n);    A.print ();//If none of the above Non-const overloads, then the const function    const A b (n) is executed here;    B.print ();    return 0;}

The above code runs the result:

Non-const 23const 12

Therefore, class member function overloading is not an absolute parameter list in which parameter types must be different. Why is this possible? This actually involves the underlying implementation of the C + + object model. When these two functions of a class are compiled, their registered names are different and therefore can exist together.

Reprint please specify the source (http://blog.csdn.net/gulansheng/article/details/45827755)

C + + member functions const overloading

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.