In Effective C++, Item 3, Scott Meyers suggests overloading operator*
for a class named Rational
:
class Rational { ... };
c
The point is that for class types, a = b
is just a shorthand to a.operator=(b)
, where operator=
is a member function. And member functions can be called on rvalues.
Note that in C++11 you can inhibit that by making operator=
lvalue-only:
class Rational
{
public:
Rational& operator=(Rational const& other) &;
// ...
};
The &
tells the compiler that this function may not be called on rvalues.