So you shouldn't be inheriting from a class with no virtual destructor but
if this was always the case then why does the compiler let you do it?

--
Best regards
Mark

Re: virtual destructor by William

William
Sat Nov 26 13:34:59 CST 2005

"Mark" <swozz_@hotmail.com> wrote in message
news:OKsiP4r8FHA.4076@tk2msftngp13.phx.gbl...
> So you shouldn't be inheriting from a class with no virtual destructor but
> if this was always the case then why does the compiler let you do it?

Well, C++ like C before it, has always given its developers enough rope to
hang themselves.

Whether that is an excuse or an explanation is another matter. :-)

Regards,
Will



Re: virtual destructor by David

David
Sat Nov 26 17:01:16 CST 2005


"Mark" <swozz_@hotmail.com> wrote in message
news:OKsiP4r8FHA.4076@tk2msftngp13.phx.gbl...

> So you shouldn't be inheriting from a class with no virtual
> destructor but if this was always the case then why does the
> compiler let you do it?

Depends what you mean by "shouldn't". :-)

Sometimes you want to manipulate an object using a pointer to a base
class and you need a virtual destructor.

Sometimes you just want an object with all the properties of another
one and a few more, so you derive it, and always manipulate the
derived object itself. You can get by happily without a virtual
destructor. But if you're in any doubt that you might want to do
something else - then make the destructor virtual.

Just my (probably heretic) 2d worth.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm





Re: virtual destructor by Alex

Alex
Sun Nov 27 11:02:49 CST 2005

Mark wrote:
> So you shouldn't be inheriting from a class with no
> virtual destructor but if this was always the case then
> why does the compiler let you do it?

Because...

1. ...following C++ philosophy, you don't pay for what you
don't use. If I won't use polymorphism for some class, then
I should not pay for overhead.

2. ...backward compatibility with C must be maintained. I
should be able to declare plain vanilla struct which is
compatible with C. Virtual methods (and any methods at all)
will break this contract.



Re: virtual destructor by Ulrich

Ulrich
Mon Nov 28 01:57:43 CST 2005

Mark wrote:
> So you shouldn't be inheriting from a class with no virtual
> destructor [...]

There is no such rule. There is a part of the standard that says that you
need a virtual destructor if you plan to _polymorphicaly_ delete an object.
There are ways to use inheritance and not need a virtual dtor, i.e. when
the dtor is not publicly accessible in the baseclass or when the baseclass
itself is not publicly accessible from a reference to the derived class.

> but if this was always the case then why does the compiler let you
> do it?

I hope this answers your question.

Uli