[C++] Operator Overloading

Operator overloading can be a bless or a curse. Some developers really like it and others think it just makes code confusing. You can create functions rather than overloading operators and do the exactly same tasks. If the operation makes sense, the operator overloading can make code simpler and easy to read. 

 

1.Overloading Binary Operators

The basic syntax is to create a function using the “operator” keyword.

class X {

  X operator op ( X xx );

 }

class MyClass
{
public:
  int x;
  MyClass operator +(MyClass myObj);
  MyClass operator -(MyClass myObj);
};

MyClass MyClass::operator +(MyClass myObj)
{
  MyClass tempObj;
  tempObj.x = this->x + myObj.x;
  return tempObj;
}

MyClass MyClass::operator -(MyClass myObj)
{
  MyClass tempObj;
  tempObj.x = this->x - myObj.x;
  return tempObj;
}

int main()
{
  MyClass obj1;
  MyClass *pObj2 = new MyClass();
  MyClass obj3;

  obj1.x = 20;
  pObj2->x = 10;

  obj3 = obj1 + *pObj2;
  cout << obj1.x << " + " << pObj2->x << " = " << obj3.x << endl;  // 20 + 10 = 30

  obj3 = obj1 - *pObj2;
  cout << obj1.x << " - " << pObj2->x << " = " << obj3.x << endl; // 20 - 10 = 10

  delete pObj2;
  return (EXIT_SUCCESS);
}

 

2. Overloading the Assignment Operator(=)

Overriding assignment operator is like overriding binary operators. But rather than returning a new object, it returns “this” (the object itself).

class MyClass
{
public:
  int x;
  MyClass operator =(int i);
  MyClass operator =(MyClass myObj);
};

MyClass MyClass::operator =(int i)
{
  this->x = i;
  return *this;
}

MyClass MyClass::operator =(MyClass myObj)
{
  this->x = myObj.x;
  return *this;
}

int main()
{
  MyClass obj1;
  MyClass *pObj2 = new MyClass();

  obj1 = 20;   // assign integer
  cout << obj1.x << endl;  // 20

  pObj2->x = 10;
  obj1 = *pObj2;
  cout << obj1.x << endl;  // 10

  delete pObj2;
  return (EXIT_SUCCESS);
}

 

3. Overloading Relational Operators

The relational operators are binary operators and return “bool“.

class MyClass
{
public:
  int x;
  bool operator==(MyClass a);
  bool operator<(MyClass a);
  bool operator>(MyClass a);
};

bool MyClass::operator==(MyClass a) // overload ==
{
  if (x == a.x)
    return true;
  else
    return false;
}

bool MyClass::operator<(MyClass a) // overload <
{
  if (x < a.x)
    return true;
  else
    return false;
}

bool MyClass::operator>(MyClass a) // overload >
{
  if (x > a.x)
    return true;
  else
    return false;
}

int main()
{
  MyClass obj1, obj2;
  obj1.x = 40; obj2.x = 20;
  cout << "(obj1 == obj2) : " << (obj1 == obj2) << endl; // 0
  cout << "(obj1 > obj2) : " << (obj1 > obj2) << endl; // 1

  return (EXIT_SUCCESS);
}

 

4. Overloading Unary Operators

There are 2 versions: prefix and postfix

class X {

  X operator op (); // prefix

  X operator op (int notused); // postfix

}

class MyClass
{
public:
  int x;
  MyClass operator ++();
  MyClass operator ++(int notused);
};

MyClass MyClass::operator ++() // prefix
{
  ++(this->x);
  return *this;
}

MyClass MyClass::operator ++(int notused) // postfix
{
  MyClass tempObj = *this; // create another object and return it without changing values
  (this->x)++;
  return tempObj;
}

int _tmain(int argc, _TCHAR* argv[])
{
  MyClass obj1, obj2, obj3;

  obj1.x = 20;
  obj2 = ++obj1;
  cout << " Prefix --- " << obj2.x << " : " << obj1.x << endl;  // 21, 21

  obj1.x = 20;
  obj3 = obj1++;
  cout << " Postfix --- " << obj3.x << " : " << obj1.x << endl; // 20, 21
  return (EXIT_SUCCESS);
}

 

5. Operator Overloading Tips and Restrictions

(1) Tips

  • Overload operators only when the operation makes sense
  • Overload relational operators in pair (such as “>” and “<“)

(2) Restrictions

  • Cannot change the precedence of any operator
  • Cannot change the number of operands required by an operator
  • Some operators cannot be overloaded: . (dot), ?,  ::

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s