Can a class still be pure abstract if it has a non-pure destructor?

坚强是说给别人听的谎言 提交于 2019-12-06 06:45:00

An Abstract class must contain atleast one pure virtual function.

Your class already has two pure virtual functions run() and squeak(), So your class is Abstract because of these two pure virtual functions.

You cannot create any objects of this class.

EDIT:

A pure abstract class, is a class that exclusively has pure virtual functions (and no data). Since your destructor is not pure virtual your class is not Pure Abstract Class.

A destructor is required for every class by the rules of C++. If you don't provide one, the compiler will generate one for you.

In my opinion this is still a pure abstract class, because the destructor is an exception to the rule.

The virtual keyword means something a bit different for destructors. When a base class's dtors are virtual, it means all dtors in the inheritance hierarchy are called. You can't really override it as such.

Usually you'd expect it to be empty:

class Rodent {
public:
  virtual ~Rodent() {}
  virtual void run() = 0;
  virtual void squeak() = 0;
};

I see very little difference. It is possible that because the empty body is a no-op the compiler can ignore it through some language lawyer statute.

I don't think you'd confuse anyone by calling yours pure virtual.

As far as I understand, the C++ standard does not specify a pure abstract class. The C++0x (n3290) however specifies abstract class.

10.4/2 A class is abstract if it has at least one pure virtual function.

The pure abstract class is a conventional term and describes an abstract class that;

  • does not have of data members
  • does not have of any non-pure virtual functions
  • does not have of any concrete functions

specified by user.

So, according to this convention, the class Rodent is not a pure abstract class.

Consider implementing your interface like so

class Rodent {
public:
  virtual ~Rodent() = 0;
  virtual void run() = 0;
  virtual void squeak() = 0;
};
inline Rodent::~Rodent() {}

Specifying your destructor as pure virtual and inlining the implementation avoids the following warning in MSVC2010 when exporting subclasses:

warning C4275: non dll-interface class 'A' used as base for dll-interface class 'B'

Yes, it is not a pure class anymore. A pure, abstract class has no functionality in it, it just provides a framework. cout is functionality.

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