next up previous
Next: Method-only operators Up: How do I redefined Previous: How do I redefined

Method/function operators

The following operators can be redefined with a method or a function: binary $ +$, unary $ +$, binary $ -$, unary $ -$, $ *$, $ /$, $ \%$, $ ==$, $ !=$, $ \&\&$, $ \vert\vert$, $ !$, $ <$, $ >$, $ <=$, $ >=$, $ +=$, $ -=$, $ *=$, $ /=$, $ \%=$, $ \&$, $ \vert$, $ \wedge$, $ \sim$, $ «$, $ »$, $ \&=$, $ \vert=$, $ ++$, $ -$. The advantage of a function operator over a method operator is the possibility to define it independently from the class. For example, to use a method:
struct   Number
{
  Number  operator+(const Number &n) const { /* ... */ }
  Number  operator-() const { /* ... */ }
  bool    operator==(const Number &n) const { /* ... */ }
  Number  &operator+=(const Number &n) { /* ... */ }
  Number  &operator++() { /* ... */ }   // postfixed
  Number  operator++(int) { /* ... */ } // prefixed
  double  &operator*() { /* ... */ }    // prefixed
  double  &operator->() { /* ... */ }   // prefixed
};

struct   Stream
{
  Stream   &operator<<(const Number &n) { /* ... */ }
};
They can also be redefined with a function (possibly friend). For example:
Number  operator+(const Number &n0, const Number &n1) { /* ... */ }
Number  operator-(const Number &n) { /* ... */ }
bool    operator==(const Number &n0, const Number &n1) { /* ... */ }
Stream  &operator<<(Stream &is, const Number &n) { /* ... */ }
Number  &operator+=(Number &n0, const Number &n1) { /* ... */ }
Number  &operator++(const Number &n) { /* ... */ }     // postfixed
Number  operator++(const Number &n, int) { /* ... */ } // prefixed
double  &operator*(const Number &n) { /* ... */ }      // prefixed
double  &operator->(const Number &n) { /* ... */ }     // prefixed



Alexis Angelidis (PhD) 2005-01-11