如果它有一个非纯析构函数,一个类仍然是纯抽象的吗? [英] Can a class still be pure abstract if it has a non-pure destructor?

查看:152
本文介绍了如果它有一个非纯析构函数,一个类仍然是纯抽象的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一项练习,要求我使用一个基类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?

推荐答案

必须包含至少一个纯虚拟功能。

您的类已有两个纯虚函数 run() squeak / code>,所以你的类是Abstract,因为这两个纯虚函数。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆