FrozenHeart
FrozenHeart

Reputation: 20766

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.

Upvotes: 8

Views: 180

Answers (5)

juanchopanza
juanchopanza

Reputation: 227548

If you mean "when are the two syntaxes not equivalent", then the answer is when the equality operator is not a member of whatever type first is. Obviously the second variant will not work for types without a member operator==. That includes built-in types.

When a member operator does exist for the type of first, then the two differ because the non-member operator could allow type conversions for both first and second, whereas the example with the member operator can only allow a conversion for second.

Upvotes: 3

didierc
didierc

Reputation: 14750

If your question is based on syntax alone, then it depends on the type of the parameters. Consider the following code:

#include <iostream>

using namespace std;

class a {


public:

  a(){
  }

  int operator ==(const a& v){
    cout << "member operator" << endl;
    return false;
  }

};

int operator ==(a &v1, a &v2){
    cout << "external operator" << endl;
    return true;
}


int main(){

  a a1, a2;

  if (a1 == a2 && a1.operator==((const a&)a2)) {
    cout << "done" << endl;
  }

}

In this setting, both operators are being called. If the function type parameters are the same, it seems that the method will be prefered.

Upvotes: 0

Murilo Vasconcelos
Murilo Vasconcelos

Reputation: 4827

One illustration to a situation in which the two statement have different effects is where you have implicit conversions of first. For example:

struct my_int
{
    int num;

    my_int(int num) : num(num) {}
};

bool operator==(const my_int& a, const my_int& b)
{
    return a.num == b.num;
}

In this case, the following is a valid C++ code:

my_int a(1);
int x = 1;

if (x == a) {
    std::cout << "Equal\n";
}

Where compiling this code:

my_int a(1);
int x = 1;

if (x.operator==(a)) {
    std::cout << "Equal\n";
}

Gives a compile error like the following:

conversion.cpp: In function ‘int main()’: conversion.cpp:21:16: error: request for member ‘operator==’ in ‘x’, which is of non-class type ‘int’

Upvotes: 1

aschepler
aschepler

Reputation: 72443

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.

Upvotes: 9

David G
David G

Reputation: 96845

The second won't work with primitives. However, both forms will work with custom-built types who have that operator overloaded publicly.

Upvotes: 0

Related Questions