【发布时间】:2015-08-08 15:14:02
【问题描述】:
假设我们有一个像下面这样的层次结构。我们是否必须调用虚拟基类 A 的 operator = 方法?
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;
}
}
【问题讨论】:
-
取决于
B/C::operator的实现。他们可能确实打电话给A::operator =。 -
D只有两个父类(B和C)不要在复制操作符中调用A(但是,在构造过程中,最派生类必须调用虚基构造函数)
标签: c++ oop operator-overloading base-class virtual-inheritance