Overloading assignment operators when a class is a child

How do you set base class members using an implementation of an assignment operator? If, for example, someone defines an assignment operator in a derived class as follows:

(where both colour and Colour() are members of the base class - this means that the lines below are illegal)

 Derived& Derived::operator=(const Derived& rhs) { if (&rhs != this) { Colour(rhs.colour); // not allowed Colour(rhs.Colour()); // not allowed } return *this; } 

what solution? Is there a way to link operator overloads in the database? I'm doing something like ...

 Derived& Derived::operator=(const Derived& rhs) : Base::operator=(rhs) ...? 
+6
source share
4 answers

You are close, just put this call into the body of the method.

  if (&rhs != this) { Base::operator=(rhs); // ... 
+3
source

This is done as follows:

 class B { public: B& operator=( const B & other ) { v = other.v; return *this; } int v; }; class D : public B { public: D& operator=( const D & other ) { B::operator=( other ); return *this; } }; 
+6
source

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; } 
+1
source

I implement the operator = assignment / construction of color in the operator of the base class, if you want to call the Base = operator from the Derived class:

 Base::operator=(rhs) 

in Derived operation of operator = () class. As far as I know, the signature you proposed for Derived operator = is invalid C ++.

+1
source

Source: https://habr.com/ru/post/903392/


All Articles