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.