关于隐式删除的虚拟析构函数的错误消息是什么?
我刚刚在 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 类以抑制这些错误的解决方案,但仍然有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
父类析构函数始终需要从子类中调用(因为这是自动发生的),因此父类析构函数不能是私有的。
只需保护您的
NonInstantiable
的析构函数即可。另请注意,子类可以通过显式(意外?)调用其公共编译器生成的复制构造函数来绕过父类。
编辑:我应该补充一点,您可能想在这里考虑对不可实例化类的需求。我个人认为,自由函数和匿名命名空间变量的组合将是一种更简洁的方法。
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.