Consider the assignment operators in classes Parent
and Child
, below.
#include <iostream>
class Parent
{
public:
Parent(){};
virtual ~Parent(){};
Parent& operator=(const Parent& other){mP = other.mP; return *this;};
void setP(double inP){mP = inP;};
double getP(){return mP;};
protected:
double mP;
};
class Child : public virtual Parent
{
public:
Child(){};
virtual ~Child(){};
Child& operator=(const Child& other)
{
mC = other.mC;
mP = other.mP;// this line
return *this;
};
void setC(double inC){mC = inC;};
double getC(){return mC;};
protected:
double mC;
};
Is here a way to avoid the duplicate line mP = other.mP;
?
The reason I am asking is that as the number of bases get higher and the inheritance structure gets more complicated, it is easy to lose track of members.
EDIT
The reason I need to implement the operator=
is that it needs to check some things before the assignments.
Just call the Parent operator:
Or really, don't implement either operator since both are trivial!