【发布时间】:2019-05-27 03:39:31
【问题描述】:
我正在尝试覆盖 C++ 中的虚函数。在我覆盖该函数后,它实际上并没有覆盖它,因此使类抽象。 下面的代码将使您对问题有一个很好的理解。
正如您在下面看到的,该代码适用于非指针模板(如 int),但因 int 指针而失败。
我想可能是因为指针引用有问题,所以我在 Derived2 的实现中去掉了 & ,但这并没有解决它。
template<class T>
class Base {
virtual void doSomething(const T& t) = 0;
};
class Derived1: public Base<int>{
void doSomething(const int& t) {
} // works perfectly
};
class Derived2: public Base<int*>{
void doSomething(const int*& t) {
}
// apparently parent class function doSomething is still unimplemented, making Derived2 abstract???
};
int main(){
Derived1 d1;
Derived2 d2; // does not compile, "variable type 'Derived2' is an abstract class"
}
【问题讨论】:
标签: c++ templates inheritance polymorphism overriding