Base::operator=
calls the Base
class version of operator=
to copy the parts of the object that are part of Base
and then the derived class copies the rest. Since the base class already does all the work required correctly why repeat the work, just reuse. So let's take this simplified example:
class Base
{
std::string
s1 ;
int
i1 ;
public:
Base& operator =( const Base& b) // method #1
{
// trvial probably not good practice example
s1 = b.s1 ;
i1 = b.i1 ;
return *this ;
}
} ;
class Derived : Base
{
double
d1 ;
public:
Derived& operator =(const Derived& d ) // method #2
{
// trvial probably not good practice example
Base::operator=(d) ;
d1 = d.d1 ;
return *this ;
}
} ;
Derived
will have three member variables, two from Base
: s1
and i1
and one of it's own d1
. So Base::operator=
calls what I have labeled as method #1
which copies the two variables that Derived
inherited from Base
which are s1
and i1
and so that all that is left to copy is d1
which is what method #2
takes care of.