GNU compiler warning “class has virtual functions but non-virtual destructor”

前端 未结 7 2346
有刺的猬
有刺的猬 2020-12-13 06:08

I have defined an interface in C++, i.e. a class containing only pure virtual functions.

I want to explicitly forbid users of the interface to delete the object thro

相关标签:
7条回答
  • 2020-12-13 06:11

    If you insist on doing this, go ahead and pass -Wno-non-virtual-dtor to GCC. This warning doesn't seem to be turned on by default, so you must have enabled it with -Wall or -Weffc++. However, I think it's a useful warning, because in most situations this would be a bug.

    0 讨论(0)
  • 2020-12-13 06:14

    If the destructor is virtual it makes sure that the base class destructor is also called fore doing the cleanup, otherwise some leaks can result from that code. So you should make sure that the program has no such warnings (prefferably no warnings at all).

    0 讨论(0)
  • 2020-12-13 06:18

    It's an interface class, so it's reasonable you should not delete objects implementing that interface via that interface. A common case of that is an interface for objects created by a factory which should be returned to the factory. (Having objects contain a pointer to their factory might be quite expensive).

    I'd agree with the observation that GCC is whining. Instead, it should simply warn when you delete an ITest*. That's where the real danger lies.

    0 讨论(0)
  • 2020-12-13 06:21

    If you had code in one of ITest's methods that tried to delete itself (a bad idea, but legal), the derived class's destructor wouldn't be called. You should still make your destructor virtual, even if you never intend to delete a derived instance via a base-class pointer.

    0 讨论(0)
  • 2020-12-13 06:26

    It's more or less a bug in the compiler. Note that in more recent versions of the compiler this warning does not get thrown (at least in 4.3 it doesn't). Having the destructor be protected and non-virtual is completely legitimate in your case.

    See here for an excellent article by Herb Sutter on the subject. From the article:

    Guideline #4: A base class destructor should be either public and virtual, or protected and nonvirtual.

    0 讨论(0)
  • 2020-12-13 06:27

    My personal view is that you'd doing the correct thing and the compiler is broken. I'd disable the warning (locally in the file which defines the interface) if possible,

    I find that I use this pattern (small 'p') quite a lot. In fact I find that it's more common for my interfaces to have protected dtors than it is for them to have public ones. However I don't think it's actually that common an idiom (it doesn't get spoken about that much) and I guess back when the warning was added to GCC it was appropriate to try and enforce the older 'dtor must be virtual if you have virtual functions' rule. Personally I updated that rule to 'dtor must be virtual if you have virtual functions and wish users to be able to delete instances of the interface through the interface else the dtor should be protected and non virtual' ages ago ;)

    0 讨论(0)
提交回复
热议问题