prev next prev next
  Course navigation Module 3
  Lesson 6 The operator keyword
 
Objective
Use the operator keyword to overload operators.

To overload operators, you use the operator keyword similarly to how you used it to overload functions.
For example:
 
class foo {
public:
   foo  operator-();      //overload unary minus
   foo  operator-(int);   //binary minus foo-int
   foo  operator-(foo);   //binary minus foo-foo
};

foo operator-(int, foo);   //binary minus int-foo
foo operator-(int, foo*);  //illegal:need foo or foo&
   
Notice that it is our style to omit spaces between the keyword operator, the operator to be overloaded, and the signature.
  Course navigation