Given that this is the only == function in the class,
bool myClass::operator == ( const myClass & rhs )
{
if ( var1 == rhs.var1 )
return true;
else
return false;
}
What does the following comparison assume?
myClass mc1;
anotherClass ac1;
if ( mc1 == ac1 )
My instinct was to say that it assumes that ac1 will be converted to type myClass, but how can that happen? operator== has a parameter of type myClass & rhs, so how could myClass’s conversion constructor be called when ac1 is passed to the function?
Thanks!
Since there’s no perfect overload for that operator the converting constructor is called (it counts as an implicit conversion), and the temporary created in this way is passed to your
operator==. After the call the temporary is destroyed.Notice that this wouldn’t happen if
operator==accepted a non-constreference, because temporaries can be bound only toconstreferences.