C++ g ++“因为以下虚函数是纯的”具有抽象基类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12615904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++ "because the following virtual functions are pure" with abstract base class
提问by tdihp
Here is my example code which produces the error:
这是我产生错误的示例代码:
struct Impl
{
int data_size_;
int find(int var){return 0;}
int get(int rowid){return 0;}
};
class Container
{
public:
Container() {}
virtual ~Container() {}
virtual int get_size() = 0;
virtual int get(int rowid) = 0;
};
class SortedContainer : virtual public Container {
public:
virtual int find(int var) = 0;
};
class ContainerImpl : public Container
{
protected:
Impl impl_;
public:
int get_size() {return impl_.data_size_;}
int get(int rowid) {return impl_.get(rowid);}
};
class SortedContainerImpl
: public SortedContainer, public ContainerImpl
{
private:
typedef ContainerImpl Base;
public:
int find(int var){return Base::impl_.find(var);}
};
ContainerImpl ci;
SortedContainerImpl sci;
it seems "SortedContainerImpl" went wrong while "ContainerImpl" is fine.
似乎“SortedContainerImpl”出错了,而“ContainerImpl”很好。
g++ complains:
g++ 抱怨:
example_b.cpp:42:21: error: cannot declare variable ‘sci' to be of abstract type ‘SortedContainerImpl'
example_b.cpp:32:7: note: because the following virtual functions are pure within ‘SortedContainerImpl':
example_b.cpp:13:15: note: virtual int Container::get_size()
example_b.cpp:14:15: note: virtual int Container::get(int)
I inheret SortedContainerImpl from ContainerImpl in order to reuse get_size() and get(int)
我从 ContainerImpl 继承了 SortedContainerImpl 以便重用 get_size() 和 get(int)
I'm not familiar with c++, What's the nature of this problem and How can I fix it?
我不熟悉 C++,这个问题的本质是什么,我该如何解决?
Thanks all.
谢谢大家。
采纳答案by Michael Burr
Your SortedContainerImpl
class has two separate Container
base classes. One is virtual (via the SortedContainer
class) and the other is non-virtual (via the ContainerImpl
class).
你的SortedContainerImpl
类有两个独立的Container
基类。一个是虚拟的(通过SortedContainer
类),另一个是非虚拟的(通过ContainerImpl
类)。
SortedContainerImpl
has concrete implementations of Container::get_size()
and Container::get(int)
for the base that comes in from ContainerImpl
, but not for the virtual base that comes in via SortedContainer
.
SortedContainerImpl
具有具体实现Container::get_size()
和Container::get(int)
用于从来自在基ContainerImpl
,但不为附带在经由虚拟基础SortedContainer
。
One way to fix the problem is to give concrete implementations in SortedContainerImpl
:
解决问题的一种方法是在以下内容中给出具体实现SortedContainerImpl
:
class SortedContainerImpl
: public SortedContainer, public ContainerImpl
{
private:
typedef ContainerImpl Base;
public:
int find(int var){return Base::impl_.find(var);}
int get_size() {return ContainerImpl::get_size();}
int get(int rowid) {return ContainerImpl::get(rowid);}
};
Another way would be to make Container
a virtual base class of ContainerImpl
, so SortedContainerImpl
would only get the one, virtual, base Container
:
另一种方法是创建Container
一个虚拟基类ContainerImpl
,所以SortedContainerImpl
只会得到一个, virtual, base Container
:
class ContainerImpl : virtual public Container
{
protected:
Impl impl_;
public:
int get_size() {return impl_.data_size_;}
int get(int rowid) {return impl_.get(rowid);}
};
回答by Alok Save
In C++ Once you have an pure virtual member function your class becomes abstract class and you cannot create any objects of it.
Such a class is not meant to be instantiable by itself.It is meant to act as an Interface. One would derive from such an abstract class and provide implementations of all the pure virtual functions in the derived class.
在 C++ 中,一旦您拥有纯虚拟成员函数,您的类就变成了抽象类,并且您无法创建它的任何对象。
这样的类本身并不意味着可实例化。它意味着充当Interface。可以从这样一个抽象类派生,并提供派生类中所有纯虚函数的实现。
Note that your class SortedContainerImpl
derives from two classes SortedContainer
and ContainerImpl
.SortedContainer
in turn derives from Container
but it never implements the pure virtual functions.
请注意,您的类SortedContainerImpl
派生自两个类SortedContainer
和ContainerImpl
. SortedContainer
反过来派生自Container
但它从未实现纯虚函数。