You should be able to use public accessors and mutators:
Derived& Derived::operator=(const Derived& rhs) { if (&rhs != this) SetColour(rhs.GetColour()); return *this; }
Otherwise, the members will be protected in the base class so that the resulting classes have access:
Derived& Derived::operator=(const Derived& rhs) { if (&rhs != this) colour = rhs.colour; return *this; }
The third option may be to define the assignment operator public in the base class and assign the base operator to the derived class:
Derived& Derived::operator=(const Derived& rhs) { if (&rhs != this) Base::operator=(rhs); return *this; }
Here's the full test case:
#define TEST 2 class Base { public: Base() : m_protected(0), m_private(0) {} Base(int pro, int pri) : m_protected(pro), m_private(pri) {} ~Base() {} #if TEST == 1 Base& operator=(const Base& rhs) { if (this != &rhs) { m_protected = rhs.m_protected; m_private = rhs.m_private; } return *this; } #elif TEST == 2 void SetPrivate(int i) { m_private = i; } int GetPrivate() const { return m_private; } #endif protected: int m_protected; private: int m_private; }; class Derived : public Base { public: Derived() : Base() {} Derived(int pro, int pri) : Base(pro, pri) {} #if TEST == 1 Derived& operator=(const Derived& rhs) { Base::operator=(rhs); return *this; } #elif TEST == 2 Derived& operator=(const Derived& rhs) { if (this != &rhs) { SetPrivate(rhs.GetPrivate()); m_protected = rhs.m_protected; } return *this; } #endif }; int main() { Derived a; Derived b(10, 5); a = b; return 0; }
Ajg85 source share