我试着写一个抽象类,一些纯虚二元操作,应该由派生类,以实现运营商多态性实现。 这里有一个简单的例子:
class Base {
public:
virtual const Base& operator+ (const Base&) const = 0;
};
class Derived : public Base {
public:
const Derived& operator+ (const Derived&) const;
};
const Derived& Derived::operator+ (const Derived& rvalue) const {
return Derived();
}
它不会对现在的运营商做什么无所谓,重要的部分是它返回什么:它返回一个临时的派生对象,或者对它的引用。 现在,如果我尝试编译,我得到这样的:
test.cpp: In member function ‘virtual const Derived& Derived::operator+(const Derived&) const’:
test.cpp:12:17: error: cannot allocate an object of abstract type ‘Derived’
test.cpp:6:7: note: because the following virtual functions are pure within ‘Derived’:
test.cpp:3:22: note: virtual const Base& Base::operator+(const Base&) const
怎么了? 是不是运营商+(Base中的唯一的纯虚函数)被重写? 衍生为什么应该是抽象的呢?