【发布时间】:2010-04-27 16:45:30
【问题描述】:
我有以下课程:
class A {
void commonFunction() = 0;
}
class Aa: public A {
//Some stuff...
}
class Ab: public A {
//Some stuff...
}
根据用户输入,我想创建一个 Aa 或 Ab 的对象。我的初衷是这样的:
A object;
if (/*Test*/) {
Aa object;
} else {
Ab object;
}
但是编译器给了我:
error: cannot declare variable ‘object’ to be of abstract type ‘A’
because the following virtual functions are pure within ‘A’:
//The functions...
有什么好办法解决这个问题吗?
【问题讨论】:
-
你的基类也需要一个虚拟析构函数。
标签: c++ class inheritance