🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Virtual functions and destructors

#2511
From: Bill Leonard
Date: Fri, 10 Nov 1995 20:01
60 lines
3267 bytes
In article <47u94u$4of@bigboote.WPI.EDU>, parker@WPI.EDU (Chris Parker) writes:
>    May I inquire when you would ever *not* want the destructor to be virtual?

To save time and space.  If there are no *other* virtual functions in the
class, then class objects need not contain a virtual function table
pointer, and access to a non-virtual destructor is faster.

The only time you *DO* need for a class to have a virtual destructor is
when you plan to delete objects of that class using a pointer or reference
to some base class.  There are *many* classes for which that is not true,
especially when the classes otherwise have no virtual member functions.

>     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?

This is virtually (sorry for the pun) impossible in your average C++
compilation environment.  It would require the compiler to know about all
derived classes before it could generate code for any manipulation of a
given class object.  Consider the following:

class A { A(); ~A(); }

A * a = Foo();
delete a;       // Virtual or not?

Now if some other compilation unit somewhere is suddenly going to force A
to have a virtual destructor, the compiler must add a virtual function
table pointer to class A.  Calls to class A's destructor, as in the "delete
a" above, cannot be completely compiled without global knowledge of all
classes derived from A.

Now all that is technically feasible, but is it desirable?  Not in my
opinion.  What problem are you solving by adding this requirement that
cannot be solved another way, say by making the destructors virtual
*explicitly* in the first place?

-- 
Bill Leonard
Harris Computer Systems Corporation
2101 W. Cypress Creek Road
Fort Lauderdale, FL  33309
Bill.Leonard@mail.hcsc.com

These opinions and statements are my own and do not necessarily reflect the
opinions or positions of Harris Computer Systems Corporation.

------------------------------------------------------------------------------
I feel like I've been sent for and couldn't go, and weren't needed after I
got there.
------------------------------------------------------------------------------

---
[ 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: <480av2$bcf@hawk.hcsc.com>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <47r71q$cp0@bigboote.WPI.EDU> <martelli.815930692@cadlab> <47u94u$4of@bigboote.WPI.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: bill@amber.ssd.hcsc.com (Bill Leonard)
Newsgroups: comp.std.c++
Subject: Re: Virtual functions and destructors
Date: 10 Nov 1995 20:01:48 GMT
Organization: Harris Computer Systems, Ft. Lauderdale FL
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <480av2$bcf@hawk.hcsc.com>
References: <47r71q$cp0@bigboote.WPI.EDU> <martelli.815930692@cadlab> <47u94u$4of@bigboote.WPI.EDU>
Reply-To: Bill.Leonard@hawk.hcsc.com
NNTP-Posting-Host: physics2.berkeley.edu