如果一个类有一个非纯析构函数,它还能是纯抽象的吗? [英] Can a class still be pure abstract if it has a non-pure destructor?
问题描述
我正在做一个练习,该练习要求我选取一个 Rodent 基类并使其成为一个纯抽象类.我对纯抽象类的理解是它充当接口并且只包含纯虚函数.虽然这是一个简单的练习,但我对本书提供的解决方案有疑问:
I am working on an exercise which asks me to take a base class Rodent and make it a pure abstract class. My understanding of a pure abstract class is that it acts as an interface and only contains pure virtual functions. Although this is an easy exercise I have a problem with the solution provided by the book:
class Rodent
{
public:
virtual ~Rodent() {cout << "Destroy rodent" << endl;}
virtual void run() = 0;
virtual void squeak() = 0;
};
如您所见,作者为析构函数添加了一个虚拟定义.加入这个定义是不是意味着这是一个抽象类而不是一个纯"的抽象类?
As you can see the author has added a dummy definition for the destructor. Does the adding of this definition not mean that this is an abstract class and not a 'pure' abstract class?
推荐答案
一个抽象类必须包含至少一个纯虚函数.
An Abstract class must contain atleast one pure virtual function.
你的类已经有两个纯虚函数run()
和squeak()
,所以你的类是抽象的,因为这两个纯虚函数.
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.
纯抽象类,是专门具有纯虚函数(没有数据)的类.由于您的析构函数不是纯虚拟的,因此您的类不是纯抽象类.
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.
这篇关于如果一个类有一个非纯析构函数,它还能是纯抽象的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!