say i have the following C++ class.
class C
{
int foo() const;
int & foo();
};
and i just call myC.foo(), i can see using the debugger that it calls the one with the reference.
why?
thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s likely because
myCis a non-const value and hence the compiler is prefering the non-const method. Theconstmethod will only be preferred when accessed from aconstvalue. For exampleEDIT
Also, as Oli pointed out, overload resolution in C++ is not affected by the return type of the method. So it won’t pick one of these signatures over the other based on the way in which the value is used.