Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator call syntax in C++

Whether there can be a situation where the syntax

if (first == second) // ...

is different from that?

if (first.operator==(second)) // ...

I don't think so, but just want to know it.

like image 448
FrozenHeart Avatar asked Oct 29 '12 17:10

FrozenHeart


1 Answers

a == b

is sometimes equivalent to

a.operator==(b)

and sometimes equivalent to

operator==(a,b)

and sometimes equivalent to neither, if the meaning ends up being the "built-in" meaning of == for non-class types.

Whenever the compiler sees ==, if at least one type involves a user-defined type, it searches for member operators (must not be hidden in the scope of a's class type) and non-member operators (using argument dependent lookup) and built-in meanings (since a class might have an implicit conversion to an ordinary type with built-in comparison). If more than one could make sense, it goes to the rules for overload resolution.

like image 175
aschepler Avatar answered Oct 28 '22 22:10

aschepler