gcc 这个关于隐式删除虚析构函数的错误消息是什么?

disho6za  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(132)

我刚刚更新了GCC从(我认为)4.5.6到4.6.1,在Windows下,MinGW.突然我的NonInstantiable基类(从它继承公共虚拟以防止示例化)拒绝工作,并显示以下和类似的错误信息:

#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

错误:

/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()'

我怀疑这是因为我没有在子类中创建任何析构函数,无论是虚的还是其他的,这在某种程度上与NonInstantiable的私有虚析构函数冲突,但我需要确认,以及如何修复我的NonInstantiable类以抑制这些错误,但仍然工作的解决方案。

ars1skjm

ars1skjm1#

父类析构函数总是需要从子类调用(因为这是自动发生的),因此父类析构函数不能是私有的。
只需保护NonInstantiable的析构函数。
还要注意,子类可以通过显式(意外地?)调用其公共编译器生成的复制构造函数来绕过父类。
编辑:我应该补充一句,你可能需要考虑一下你对一个不可示例化类的需求。我个人认为自由函数和匿名名称空间变量的组合会是一种更干净的方法。

相关问题