Order of resolution reload and accessibility check in C ++

Source: Internet
Author: User

For the following classes:

Class clxecs
{
Public:
Double test (double dvalue) {return dvalue * 13 ;};

PRIVATE:
Int test (INT ivalue) {return ivalue * 13 ;};
};

What is the function output below?

Void ecs_test ()
{
Int ivalue = 13;

Clxecs lx;

Cout <lx. Test (ivalue) <Endl;
}

If your answer is 169, you are wrong!
Because the above functions cannot be compiled at all! The compiler will give you an error that cannot access private members.
Surprised? Can the compiler not find the public member function Double test (double dvalue) of clxecs and convert the real parameter ivalue type to double?
The answer is: no! The reason is simple:In C ++, the overload resolution is performed before the accessibility check.
For the above example, the compilation is performed in the following order:
First, the compiler reloads the resolution to find the appropriate member function (regardless of whether the searched member function is public ). At this time, the compiler naturally chooses the test member function of the int type.
Then, the compiler checks access. At this time, the compiler will find that the member function test with the parameter type of int is a private member of the clxecs class. Then, the compiler reports an error indicating that you cannot access private members.

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.