首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >多重继承和纯虚函数

多重继承和纯虚函数
EN

Stack Overflow用户
提问于 2012-01-02 08:10:40
回答 1查看 11.4K关注 0票数 18

以下代码:

代码语言:javascript
复制
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;
}

编译失败,出现以下错误:

代码语言:javascript
复制
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’继承是虚拟的,解决了这个问题,但我不明白为什么。有人能解释一下这是怎么回事吗?

附注:为了使代码更简短,我故意省略了虚拟析构函数。请不要告诉我把它们放进去,我已经知道了:)

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8696473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档