【发布时间】:2016-08-31 19:45:47
【问题描述】:
假设我有以下课程
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
}
};
我很想知道为什么编译器默认不能在派生赋值期间提供调用基的赋值运算符,为什么我们需要显式调用它。 我是否忽略了某些不需要此(调用 A::operator = (other) )的情况。
对于构造函数编译器有这个考虑(即在派生构造期间调用基的构造函数)。
【问题讨论】:
-
我不明白你的要求?如果您不提供自己的实现,那么所有这些都是默认提供的。
-
我认为你可以使用 en.cppreference.com/w/cpp/language/using_declaration 即:使用 Base::operator=;
标签: c++ operators variable-assignment