Wednesday, August 19, 2009

c++: op overloading: this->Parent::operator=(rhs);

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); // Or no need hiden this pointer.
. . . // copy all our own fields here.
}
return *this;
}//end operator=

No comments:

Post a Comment