Example 5-17. Calling overloaded operators
class demo
{
public:
demo(int v) : value_(v) {}
demo add(const demo& d) const;
demo sub(const demo& d) const;
demo mul(const demo& d) const;
demo operator+(const demo& d) const;
demo operator-(const demo& d) const;
demo operator*(const demo& d) const;
operator int( ) const { return value_; }
private:
int value_;
};
// Silly examples, but illustrative
demo add(const demo& a) { return a; }
demo mul(const demo& a) { return a; }
demo div(const demo& a) { return a; }
demo operator+(const demo& a, const demo& b)
{
return a.operator+(b); // Force use of member function.
}
demo demo::add(const demo& d)
const
{
return *this + d; // Error: calls ::operator+( ) or demo::operator+( )?
}
demo demo::sub(const demo& d) const
{
return this->operator-(d); // Member operator
}
demo demo::mul(const demo& d) const
{
return ::operator*(*this, d); // Global operator
}
demo demo::operator+(const demo& d) const
{
return demo(int(*this) + int(d));
}
demo demo::operator-(const demo& d) const
{
return sub(d); // Calls demo::sub (recurses infinitely)
}
demo demo::operator*(const demo& d) const
{
return ::mul(d); // Scopes operator to call global mul( )
}