Re: Is gcc warning about non-virtual destructor useless?
Frank Birbacher wrote:
Hi!
I'm using gcc 4.1.1 on linux. For the following class "Visitor" it
generates a warning (enable all warnings using g++ option "-Wall").
struct A;
struct Visitor {
virtual void visit(A*) =0;
protected:
~Visitor() {}
};
I see nothing wrong with this, since it is not possible to delete
this through the Visitor interface. It also dictates responsibility.
See Herb Sutter publication:
http://www.gotw.ca/publications/mill18.htm
Guideline #4: A base class destructor should be either public and
virtual, or protected and nonvirtual.
The warning reads "warning: 'struct Visitor' has virtual functions but
non-virtual destructor". It is warning not to try polymorphic deletion
through a base pointer.
Yes, the warning has irritated me as well. I've reverted to making
the
protected destructor virtual. IMO it should not have any runtime
impact anyway as the destructor is never called polymorphically
(never called via Visitor).
I'm raising this issue because I'm trying to compile my code cleanly at
high warning levels and this issue keeps anoying me. My current solution
is to make the dtor virtual because it doesn't impact runtime
performance significantly.
IM(h)O it should not impact at all, aside from perhaps making ones
class larger. I
don't know whether compilers could perhaps optimize the vtable on the
basis
that the function is never called polymorphically. If this was the
case the impact
would be zip.
But I'd like to see gcc improving on this if
there is consensus about not issuing the warning for protected dtors.
What is your opinion?
Agreed.
Werner
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]