How do you set base class members using the assignment operator implementation? If for example someone defines the assignment operator in a derived class like this:
(where both colour and Colour() are members of the base class – meaning the lines indicated below are illegal)
Derived& Derived::operator=(const Derived& rhs)
{
if (&rhs != this)
{
Colour(rhs.colour); // not allowed
Colour(rhs.Colour()); // not allowed
}
return *this;
}
what is the solution? Is there a way of linking operator overloads in the base? Do I do something like…
Derived& Derived::operator=(const Derived& rhs) : Base::operator=(rhs)
...?
You’re close, just put that call in the method body.