g++ “because the following virtual functions are pure” with abstract base class

匿名 (未验证) 提交于 2019-12-03 03:12:02

问题:

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.

g++ complains:

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)

I'm not familiar with c++, What's the nature of this problem and How can I fix it?

Thanks all.

回答1:

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 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.

One way to fix the problem is to give concrete implementations in 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:

class ContainerImpl : virtual public Container { protected:   Impl impl_; public:   int get_size() {return impl_.data_size_;}   int get(int rowid) {return impl_.get(rowid);} }; 


回答2:

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.

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!