For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > virtual destructor









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author virtual destructor
Mark

2005-11-26, 7:01 pm

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


William DePalo [MVP VC++ ]

2005-11-26, 7:01 pm

"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


David Webber

2005-11-26, 7:01 pm


"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




Alex Blekhman

2005-11-27, 7:04 pm

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.


Ulrich Eckhardt

2005-11-28, 3:58 am

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com