有关隐式删除的虚拟析构函数的错误消息是什么?

时间:2011-09-28 17:28:03

标签: c++ gcc c++11 virtual-destructor

我刚刚在Windows,MinGW下更新了GCC(我认为)4.5.6到4.6.1。突然,我的NonInstantiable基类(使用public virtual继承以防止实例化)拒绝使用以下和类似的错误消息:

#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类以抑制这些错误,但仍然有效。

1 个答案:

答案 0 :(得分:19)

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

让你的NonInstantiable的析构函数受到保护。

另请注意,子类可以通过显式(意外地?)调用其公共编译器生成的复制构造函数来绕过父类。

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