Trouble with inheritance of operator= in C++

前端 未结 5 1552
灰色年华
灰色年华 2020-11-27 04:20

I\'m having trouble with the inheritance of operator=. Why doesn\'t this code work, and what is the best way to fix it?

#include 

class A
{
         


        
5条回答
  •  萌比男神i
    2020-11-27 05:06

    You cannot assign across the hierarchy like this - B and C are different subclasses of A. You can assign a B to a B or a C to a C but not a C to a B or vice versa.

    You probably want to implement operator= in B and C, delegating the A part of the assignment to A::operator= before you try this though. Otherwise the B- and C-specific parts of those classes will get lost in the assignment.

提交回复
热议问题