以下代码:
struct interface_base
{
virtual void foo() = 0;
};
struct interface : public interface_base
{
virtual void bar() = 0;
};
struct implementation_base : public interface_base
{
void foo();
};
struct implementation : public implementation_base, public interface
{
void bar();
};
int main()
{
implementation x;
}编译失败,出现以下错误:
test.cpp: In function 'int main()':
test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation'
test.cpp:16:8: note: because the following virtual functions are pure within 'implementation':
test.cpp:3:18: note: virtual void interface_base::foo()我已经尝试过了,我想让'interface -> interface_base‘和'implementation_base -> interface_base’继承是虚拟的,解决了这个问题,但我不明白为什么。有人能解释一下这是怎么回事吗?
附注:为了使代码更简短,我故意省略了虚拟析构函数。请不要告诉我把它们放进去,我已经知道了:)
https://stackoverflow.com/questions/8696473
复制相似问题