C++: Overloading Derived Class Assignment

Copying parent class members

If there is a parent (base) class, those fields must also be copied. You can accomplish this with the following cryptic statement,
    this->Parent::operator=(source);
where Parent is the name of the base class.
//--- file Parent.h
class Parent {...};   // declaration of base class
//--- file Child.h
#include "Parent.h"
class Child : public Parent { // declaration of derived class
public:
    Child& Child::operator=(const Child& source);
};//end class Child
//--- file Child.cpp
#include "Child.h"
Child& Child::operator=(const Child& source) {
    if (this != &source) {
        this->Parent::operator=(source);
        . . . // copy all our own fields here.
    }
    return *this;
}//end operator=

See Also

Overloading Assignment