Item 10 - Have assignment operators return a reference to *this.
int x,y,z;
x = y = z = 15; // chain of assignments.x = (y = (z = 15));class Widget {
public:
...
Widget& operator= (const Widget& rhs) // return type is a reference to the current class
{
...
return *this;
}
...
};class Widget {
public:
...
Widget& operator+=(const Widget& rhs) // it applies even if the operator's parameter type is unconventional
{
...
return *this;
}
...
};PreviousItem 9 - Never call virtual functions during construction or destruction.NextItem 11 - Handle assignment to self in operator=.
Last updated