🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2482

Re: Virtual functions and destructors

#2482
From: Chris Parker
Date: Fri, 10 Nov 1995 05:18
35 lines
2452 bytes
In article <martelli.815930692@cadlab> martelli@cadlab.it (Alex Martelli) write
s:
>
>>Is virtualness inherited by destructors?  Does C++ automatically make all 
>>applicable superclasses also have virtual destructor if they don't already?
>
>Inheritance flows _downwards_ -- a superclass's characteristics (in
>particular, whether its dtor is or isn't virtual) is never changed by
>some other class inheriting from it.

Alex,

   Yeah, I know what you're saying, but I think when the function is the 
destructor we should consider it differently.  Normally, when you destroy
an object, each destructor up the parentage tree gets executed in order from
child to parent.  In a strange kind of way, it behaves somewhat virtually
on its own.
   May I inquire when you would ever *not* want the destructor to be virtual?
Probably only in the leaf of the class heirarchy.  Destroying the object above
the leaf child without having a virtual destructor causes nothing but a memory
leak, and quite possibly pain and hardship.  When you free an object, you
really need to free it as what it really is, not what it looks like it is.
    The only disadvantage of this is there is price to pay at runtime when 
freeing the object because we need to find the destructor function through 
dynamic binding.  So, I propose that if that if at any point in a class
heirarchy tree a destructor is declared virtual, all destructors in the tree
are virtual because that is really what you are trying to ask for.  What
do you think?
	
							Chris Parker
							cparke@sapient.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <47u94u$4of@bigboote.WPI.EDU>
Path: arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-back
References: <47r71q$cp0@bigboote.WPI.EDU> <martelli.815930692@cadlab>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!cs.mu.OZ.AU!bounce-back
From: parker@WPI.EDU (Chris Parker)
Subject: Re: Virtual functions and destructors
Message-ID: <47u94u$4of@bigboote.WPI.EDU>
Originator: fjh@munta.cs.mu.OZ.AU
Sender: news@cs.mu.OZ.AU (CS-Usenet)
Organization: Worcester Polytechnic Institute
References: <47r71q$cp0@bigboote.WPI.EDU> <martelli.815930692@cadlab>
X-Original-Date: 10 Nov 1995 01: 16:14 GMT
Date: Fri, 10 Nov 1995 05:18:02 GMT
Approved: fjh@cs.mu.oz.au
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMKLgmeEDnX0m9pzZAQG7KwF+MY3eTLhk7V2LrQhhxk3htSsehHV0amSC
 pTqfmQ7+ljqgUQtAUXfRRhgbAbgQN5ad
 =MITm