user2735714
user2735714

Reputation: 131

Operator = overloading in virtual inheritance in c++

Suppose that we have a hierarchy like the one below. Do we have to call the operator = method of the virtual base class A or not?

class A
{ ... }

class B : virtual public A
{ ... }

class C : virtual public A
{ ... }

class D : public B, public C
{
   D& operator = (const D& other)
   {
      if(this != &other)
      {
       // A::operator = (other); is this line correct???
          B::operator = (other);
          C::operator = (other);
          ....
       }
      return *this;
    }
 }

Upvotes: 1

Views: 217

Answers (1)

Lawrence Aiello
Lawrence Aiello

Reputation: 4638

Like @Emadpres stated in the comment, it depends on how operator= is treated in the hierarchy. If B and C utilize A's implementation of operator=, then you don't have to explicitly use that in D's implementation.

Keep in mind that, to keep semantics straight, you probably should use A's implementation in B and C. Trying to go all the way up the hierarchy tree when overloading is a good way to introduce tons of complexity.

Upvotes: 4

Related Questions