Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between global operator and member operator

Is there a difference between defining a global operator that takes two references for a class and defining a member operator that takes only the right operand?

Global:

class X { public:     int value; };  bool operator==(X& left, X& right)  {     return left.value == right.value; }; 

Member:

class X {     int value;     bool operator==( X& right)      {         return value == right.value;     }; } 
like image 742
Vargas Avatar asked Jul 17 '09 18:07

Vargas


People also ask

What is member operator function?

Member functions are operators and functions declared as members of a certain class. They don't include operators and functions declared with the friend keyword. If you write an operator function as a member function, it gains access to all of the member variables and functions of that class.

Which operator must be a member function?

The overloaded operator must be added as a member function of the left operand.

How does the compiler differentiate the class specific function and global function?

How does the compiler differentiates the class-specific function and a global function? To map the function call with the corresponding function definition, the compiler performs the process of name-lookup. This process yields some set of functions that have the same name and they are called candidate functions.

Are vectors operator overloaded?

Overloaded operators are implemented as functions and can be member functions or global functions. Operator overloading involving vector types is not supported. An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator.


2 Answers

One reason to use non-member operators (typically declared as friends) is because the left-hand side is the one that does the operation. Obj::operator+ is fine for:

obj + 2 

but for:

2 + obj 

it won't work. For this, you need something like:

class Obj {     friend Obj operator+(const Obj& lhs, int i);     friend Obj operator+(int i, const Obj& rhs); };  Obj operator+(const Obj& lhs, int i) { ... } Obj operator+(int i, const Obj& rhs) { ... } 
like image 90
Tim Sylvester Avatar answered Sep 20 '22 20:09

Tim Sylvester


Your smartest option is to make it a friend function.

As JaredPar mentions, the global implementation cannot access protected and private class members, but there's a problem with the member function too.

C++ will allow implicit conversions of function parameters, but not an implicit conversion of this.

If types exist that can be converted to your X class:

class Y { public:     operator X();  // Y objects may be converted to X };   X x1, x2; Y y1, y2; 

Only some of the following expressions will compile with a member function.

x1 == x2;   // Compiles with both implementations x1 == y1;   // Compiles with both implementations y1 == x1;   // ERROR!  Member function can't convert this to type X y1 == y2;   // ERROR!  Member function can't convert this to type X 

The solution, to get the best of both worlds, is to implement this as a friend:

class X {     int value;  public:      friend bool operator==( X& left, X& right )      {         return left.value == right.value;     }; }; 
like image 34
Drew Dormann Avatar answered Sep 21 '22 20:09

Drew Dormann