i.e. if I define operators == and + in my class in a private section, can they be accessible from main?
It works in MSVS 2008 and 2010 but for me it seems to be a bug in a compiler. Is it so?
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.
You will have to show the code to get a sensible explanation of why the compiler is accepting it. My guess is that you are implementing them as
friendfree functions. At any rate, for the sake of argument, assume you have:And now the explanation. In the example,
operator+is declared as a member function inside a private section, as such, access specifiers apply and unlessmainis a friend of the class it will not have access to it. On the other handoperator==is implemented as a free function (even if the definition is provided inside the class braces) and access specifiers do not apply there.The code is almost equivalent (there is a small difference when it comes to lookup) to:
Where it is much simpler to reason about accessibility of
operator==from the main function.