默认纯虚拟析构函数 [英] Default pure virtual destructor

查看:151
本文介绍了默认纯虚拟析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,我们可以声明一个析构函数自动生成:

  struct X {
virtual〜X()= default;
};此外,我们可以声明一个析构函数是纯虚函数:







$ b b $ b

  struct X {
virtual〜X()= 0;
};

我的问题是:如何声明析构函数 纯虚拟?看起来以下语法不正确:

  struct X {
virtual〜X()= 0 =
};

这不是一个:

  struct X {
virtual〜X()= 0,default;
};

不是这个:

  struct X {
virtual〜X()= 0 default;
};

编辑:
对问题的目的有一些澄清。基本上我想要一个类是不可实例化的基类,但派生类是可实例化的,那么该类必须有一个纯虚拟析构函数。但另一方面,我不想在.cpp文件中提供定义。所以我需要一些类似于 default 的机制。

解决方案

为了定义一个纯虚方法,你需要一个单独的



因此:

  struct X {
virtual〜X()= 0;
};

X ::〜X()= default;


In C++11, we are able to declare a destructor to be auto generated:

struct X {
  virtual ~X() = default;
};

Also, we can declare a destructor to be pure virtual:

struct X {
  virtual ~X() = 0;
};

My question is: how to declare the destructor to be both auto generated and pure virtual? Looks like the following syntax is not correct:

struct X {
  virtual ~X() = 0 = default;
};

Neither is this one:

struct X {
  virtual ~X() = 0, default;
};

Nor this one:

struct X {
  virtual ~X() = 0 default;
};

EDIT: Some clarification on the purpose of the question. Basically I want an empty class to be non-instantiable base class, but derived class is instantiable, then the class must have a pure virtual destructor. But on the other hand, I don't want to provide the definition in a .cpp file. So I need some sort of mechanism equivalent to default. I wonder if anyone has an idea to solve the problem.

解决方案

In order to define a pure virtual method, you need a separate definition from the declaration.

Therefore:

struct X {
    virtual ~X() = 0;
};

X::~X() = default;

这篇关于默认纯虚拟析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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