【发布时间】:2017-12-10 15:06:49
【问题描述】:
我已经看到此类问题的多个答案,但我不知道如何解决它。主要问题可能是我正在处理其他人的代码,但是嗯。这是此问题的简化示例: 我有一门课程链接了许多其他课程:
class Interface
: public system_atoms,
public system_io,
/* etc, others */
{
/* A few functions, none that matters here - none from the inherited classes redefined */
}
在 system_atoms 中,我有:
class system_atoms {
public:
virtual int init_atom(int atom_number) = 0;
virtual int check_atom_id(int atom_number) = 0;
}
我有一个应该从接口继承的类:
class Interface_proxy : public Interface {
public:
/* stuff - no function from the inherited classes redefined */
}
在.cpp中:
Interface_proxy global_interface_proxy;
编译时会出现错误提示“无法将变量 global_interface_proxy 声明为抽象类型 Interface_proxy,因为虚函数 [system_atoms 中列出的两个] 是纯函数”。
我也不是很精通 C++。我想我应该在某处将两个虚函数重新定义为纯函数(没有= 0 和virtual 对吗?),这样变量就不是抽象类型。但是我的知识到此为止——多重继承让我感到困惑。
【问题讨论】:
-
如果该类具有纯虚方法,则不能创建该类的实例。它被称为抽象类。所有派生类都必须实现这些方法,否则它们也将是抽象的。
-
即使没有多重继承,你也会得到同样的错误。如果你想声明一个
Interface_proxy,它需要是一个指针或引用,然后你必须指向或引用实际的非抽象(例如Interface_proxy),它确实实现了纯fns。
标签: c++ inheritance abstract-class abstract