c++中虚继承中的Operator =重载
Operator = overloading in virtual inheritance in c++
假设我们有一个如下所示的层次结构。是否必须调用虚基类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;
}
}
就像@ emadpress在评论中所说的,这取决于operator=
在层次结构中的处理方式。如果B和C使用A对operator=
的实现,那么你不必在D的实现中显式地使用它。
请记住,为了保持语义的直接性,您可能应该使用A在B和c中的实现。当重载是引入大量复杂性的好方法时,尝试沿着层次结构树一路向上。
相关文章:
- 运算符重载:"operator+"必须采用零个或一个参数
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- 重载的 operator() 在 Cython 中失败
- C++ 继承:基类中重载 operator+ 的 2 次在派生类中无法正常工作
- 当试图交换可变模板类时,如何正确地重载operator=
- 错误:使用复制和交换习惯用法的交换函数中"operator="的重载不明确
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 如何在不重载"operator()"、"std::less"、"std
- 如何在具有动态大小数组的模板化类中重载 operator=
- 重载 operator= 时返回引用和值有什么区别?
- 重载Operator =作为成员函数
- 在 STL 容器中存储具有重载"operator&"的类对象在 C++ 中合法吗?
- 重载operator<对于指向类对象的指针
- 重载operator< & lt;作为会员打印
- c++重载:operator =的重载
- 是否有一种方法可以重载operator=来为右边的函数提供特定的行为?
- 声明一个接受并返回` `流` `的函数有什么用?而不是重载operator<<
- 如何重载operator<<用于c++中的数组
- 重载运算符的模板:错误:重载'operator*'必须至少有一个类或枚举类型的参数
- 为什么在 std::string 上重载"operator <<"不起作用?