JamesGold
JamesGold

Reputation: 815

Is a conversion constructor called for this overloaded operator? (C++)

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!

Upvotes: 1

Views: 395

Answers (6)

Andrew Tomazos
Andrew Tomazos

Reputation: 68668

The compiler given mc1 == ac1 will search for the best match for the operator==(A,B) using the normal overload resolution rules for any function.

Assuming it finds your bool myClass::operator==(const myClass&) function the unambiguously best match (this may not be the case. for example it may find an operator declared bool operator==(const myClass&, const anotherClass&) which is superior instead), it will then bind and make the necessary parameter conversions as it would for any function call.

To make the conversion from an lvale of type anotherClass to a const myClass& (assuming anotherClass does not inherit from myClass, in which case no conversion would be needed) it will then search for a single (unambiguously best) converting constructor or conversion operator to turn the parameter into a myClass temporary and then execute the operator== call with that.

It the parameter of a function is a non-const reference than it will not consider performing such temporary conversion. The reason is that a non-const reference usually indicates that the function will perform some sideeffect on that parameter, which would be discarded when the temporary is destroyed leaving the original object uneffected - therefore it would most likely be a logic error accidently discarding this sideeffect than intentionally doing so - so the language designers disallowed it.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168646

There are two other possible answers to your riddle, neither involving conversation constructors.

  1. There exists a free function, bool operator==(const myClass&, const &anotherClass);, or

  2. anotherClass is publicly derived from myClass.

Upvotes: 0

Matteo Italia
Matteo Italia

Reputation: 126837

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-const reference, because temporaries can be bound only to const references.

Upvotes: 1

rodrigo
rodrigo

Reputation: 98398

The parameter is of type const myClass & so a conversion call is called, a temporary is constructed and a const-reference to such a temporary is passed to the function. Upon return, the temporary is destroyed.

Upvotes: 3

Becuzz
Becuzz

Reputation: 6866

Your assumption is correct. The compiler will put it the conversion constructor call first, then call your == method with the converted object as the rhs argument.

Upvotes: 0

Yochai Timmer
Yochai Timmer

Reputation: 49251

The compiler takes the left hand side and tries to find the right operator for it.

So it will take the == operator, and try to fit the right hand side to a myClass by casting.

If it can't find implicit casting to myClass it will return a compilation error.

Upvotes: 1

Related Questions