Вы должны иметь возможность использовать открытые методы доступа и мутаторы:
Derived& Derived::operator=(const Derived& rhs)
{
if (&rhs != this)
SetColour(rhs.GetColour());
return *this;
}
В противном случае сделать члены защищенными в базовом классе, чтобы у производных классов был доступ:
Derived& Derived::operator=(const Derived& rhs)
{
if (&rhs != this)
colour = rhs.colour;
return *this;
}
Третийопцией может быть определение общедоступного оператора присваивания в базовом классе, и чтобы ваш производный класс вызывал базовый оператор:
Derived& Derived::operator=(const Derived& rhs)
{
if (&rhs != this)
Base::operator=(rhs);
return *this;
}
Вот полный тестовый пример:
#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;
}