OOPortal 





Operator Overloading  «Prev  Next»
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.
Overloadable operators
+    -    *    /    =    <    >    +=   -=   *=   /=   <<   >>
<<=  >>=  ==   !=   <=   >=   ++   --   %    &    ^    !    |
~    &=   ^=   |=   &&   ||   %=   []   ()   ,    ->*  ->   new 
delete    new[]     delete[]