假设我有以下课程
class Base
{
// default CTOR
// default Copy Assignment
};
class Derived : public Base
{
public:
Derived()
// Calls base constructor
{
// implementation
}
Dervied& operator=(const Dervied& other)
{
A::operator = (other); // Why can't compiler offer this by default
// implementation
}
};
我很想知道为什么编译器默认情况下在派生赋值期间不能提供调用base的赋值运算符,为什么我们需要显式调用它。 我是否忽略了一些不需要这种情况的情况(调用A :: operator =(other))。
对于构造函数,编译器有这种考虑(即在派生构造期间调用base的构造函数)。
答案 0 :(得分:0)
我很想知道为什么编译器默认情况下在派生赋值期间不能提供调用base的赋值运算符
一旦你要提供自己的实现,编译器就不会猜测你真正想要实现的目标。
虽然调用A::operator = (other)
看起来像是一个合理的默认操作,但您仍然可以实现不同的操作。编译器无法知道,也不会限制您,也不会影响您。
为什么我们需要明确地调用它。我是否忽略了一些不需要这种情况的情况(调用A :: operator =(other))。
只要有创意,我就能想象你实际上想要覆盖这种行为的情况。