什么是关于隐式删除的虚拟析构函数的错误消息? [英] What is this error message about implicitly deleted virtual destructors?

查看:3313
本文介绍了什么是关于隐式删除的虚拟析构函数的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚从Windows(MinGW)更新了GCC(我认为)4.5.6到4.6.1。突然间,我的NonInstantiable基类(从中继承,使用公共虚拟来阻止实例化)拒绝使用以下类似的错误消息:

  #ifndef Frigo_Lang_NonInstantiable 
#define Frigo_Lang_NonInstantiable

命名空间Frigo
{
命名空间Lang
{

/ **
*如果您想创建一个不可实例化的类,则继承此类。大多数
*对静态类很有用。看来每个继承组合
*(public / protected / private,non-virtual / virtual)都会关闭
*所有子类中的实例化。
** /

类不可证实
{
私人:
/ *私人班* /

/ **
*一个虚拟类,用于防止关于虚拟
*构造函数/析构函数和无朋友的GCC警告
** /
class NonInstantiableDummy {};

私有构造函数* /

/ **
*防止实例化的私有构造函数
** /
NonInstantiable(){ }

/ **
*私有析构函数来防止堆栈上的实例化。虚拟到
*防止GCC警告
** /
虚拟〜NonInstantiable(){}

/ *朋友* /
朋友类NonInstantiableDummy;
};

}
}

#endif

错误:


$ b

/ code / Frigo / Util / Arrays:40:7:error :删除函数'虚拟Frigo :: Util :: Arrays ::〜Arrays()'
/ code / Frigo / Lang / Object:37:11:错误:覆盖非删除函数'虚拟Frigo :: Lang: :Object ::〜Object()'
/ code / Frigo / Util / Arrays:40:7:error:'虚拟Frigo :: Util :: Arrays ::〜Arrays()'被隐式删除,因为默认定义将不合格:
/ code / Frigo / Lang / NonInstantiable:39:11:error:'虚拟Frigo :: Lang :: NonInstantiable ::〜NonInstantiable()'是私人
/ code / Frigo / Util / Arrays:40:7:error:在这种情况下
/ code / Frigo / Lang / NonInstantiable:39:11:error:'virtual Frigo :: Lang :: NonInstantiable ::〜NonInstantiable() '是private
/ code / Frigo / Util / Arrays:40:7:error:在这种情况下
/code/Frigo/ Util / Arrays:40:7:error:deleted function'virtual Frigo: :的Util ::数组::〜阵列( )'
/ code / Frigo / Lang / NonInstantiable:39:11:error:覆盖非删除函数'虚拟Frigo :: Lang :: NonInstantiable ::〜NonInstantiable()'

我怀疑这是因为我没有在子类中创建任何析构函数(虚拟的或其他的),并且这与某些私有虚拟相冲突NonInstantiable的析构函数,但我需要确认。解决方案如何修复我的NonInstantiable类来抑制这些错误,但仍然有效。

解决方案

父解析器总是需要可调用的从一个子类(因为这会自动发生),所以父类的析构函数不能是私有的。



只要让你的 NonInstantiable

还要注意,一个子类可以绕过由显式(意外地)写入其公共编译器生成的拷贝构造函数。



编辑:我应该补充一下,你可能想考虑你在这里需要一个不可实例化的类。我个人认为,自由函数和匿名命名空间变量的组合将是更简单的方法。


I've just updated GCC from (I think) 4.5.6 to 4.6.1, under Windows, MinGW. Suddenly my NonInstantiable base class (from which you inherit with public virtual to prevent instantiation) refuses to work with the following and similar error messages:

#ifndef Frigo_Lang_NonInstantiable
#define Frigo_Lang_NonInstantiable

namespace Frigo
{
namespace Lang
{

/**
*   Inherit from this class if you want to make a non-instantiable class. Most
*   useful for static classes. It seems every inheritance combination
*   (public/protected/private, non-virtual/virtual) shuts off instantiation in
*   all subclasses as well.
**/

class NonInstantiable
{
private:
/*  Private Classes  */

    /**
    *   A dummy class to prevent GCC warnings about virtual
    *   constructors/destructors and no friends
    **/
    class NonInstantiableDummy { };

/*  Private Constructors  */

    /**
    *   Private constructor to prevent instantiation
    **/
    NonInstantiable() { }

    /**
    *   Private destructor to prevent instantiation on the stack. Virtual to
    *   prevent GCC warnings
    **/
    virtual ~NonInstantiable() { }

/*  Friends  */
    friend class NonInstantiableDummy;
};

}
}

#endif

Errors:

/code/Frigo/Util/Arrays:40:7: error: deleted function 'virtual Frigo::Util::Arrays::~Arrays()'
/code/Frigo/Lang/Object:37:11: error: overriding non-deleted function 'virtual Frigo::Lang::Object::~Object()'
/code/Frigo/Util/Arrays:40:7: error: 'virtual Frigo::Util::Arrays::~Arrays()' is implicitly deleted because the default definition would be ill-formed:
/code/Frigo/Lang/NonInstantiable:39:11: error: 'virtual Frigo::Lang::NonInstantiable::~NonInstantiable()' is private
/code/Frigo/Util/Arrays:40:7: error: within this context
/code/Frigo/Lang/NonInstantiable:39:11: error: 'virtual Frigo::Lang::NonInstantiable::~NonInstantiable()' is private
/code/Frigo/Util/Arrays:40:7: error: within this context
/code/Frigo/Util/Arrays:40:7: error: deleted function 'virtual Frigo::Util::Arrays::~Arrays()'
/code/Frigo/Lang/NonInstantiable:39:11: error: overriding non-deleted function 'virtual Frigo::Lang::NonInstantiable::~NonInstantiable()'

I suspect it is because I do not create any destructors, virtual or otherwise, in the child classes, and this somehow conflicts with the private virtual destructor of NonInstantiable, but I need confirmation. And a solution how to fix my NonInstantiable class to suppress these errors, but still work.

解决方案

Parent destructors always need to be callable from a child class (because this happens automatically) and so parent class destructors can't be private.

Just make your NonInstantiable's destructor protected.

Also note that a child class could circumvent the parent as written by explicitly (accidentally?) calling into its public compiler-generated copy constructor.

EDIT: I should add as an aside that you might want to consider your need for a non-instantiable class here. I personally believe that a combination of free functions and anonymous-namespace variables would be a cleaner way of doing this.

这篇关于什么是关于隐式删除的虚拟析构函数的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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