Re: Is gcc warning about non-virtual destructor useless?
Frank Birbacher <bloodymir.crap@gmx.net> writes:
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() {}
};
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.
I don't see how one is supposed to "delete" a Visitor*. The destructor
is protected and therefore can only be called from derived classes.
The Visitor itself should know not to do "delete this;". And I think
this is enough protection against the "base pointer deletion
bug". (With enough effort one can always shoot oneself in the foot,
I know)
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. 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?
My opinion is that the compiler should produce a diagnostic only when
your code actually evaluates a delete expression where the argument is
of type Visitor *.
And that the protected-ness of the destructor is irrelevant to this
question. Even if it were private could you evaluate such a delete
expression.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]