🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Virtual functions and destructors

#2509
From: Matt Austern
Date: Fri, 10 Nov 1995 19:58
32 lines
2181 bytes
In article <199511091835.NAA11661@starling.WPI.EDU> Chris Parker <parker@owl.WPI.EDU> writes:

> I did mean superclass when I said it.  Based on what I've learned by thinking
> about these things, I'm pretty convinced that all destructors should be
> virtual all the time (implied).  I can't find a single case where it would 
> make sense to destroy an object and not have all proper cleanup happen.

This falls into the category of "if you don't use it, you shouldn't
pay for it."  A class that has virtual functions is more expensive
than a class that doesn't have any; in some cases that overhead can be
prohibitive.

So that's the case in which you want a non-virtual destructor: if the
extra overhead of virtual functions would be a problem, and if you
don't expect your class to be used as a base for inheritance, then you
shouldn't make the destructor virtual.  This is reasonably common;
consider, for example, complex<double>.  That's why the standard
allows non-virtual destructors.

It probably is true that if you have any virtual functions at all then
you should have a virtual destructor.  Most compilers do issue
warnings if you have a polymorphic class without a virtual destructor.
-- 
  Matt Austern                             He showed his lower teeth.  "We 
  matt@physics.berkeley.edu                all have flaws," he said, "and 
  http://dogbert.lbl.gov/~matt             mine is being wicked."

---
[ 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: <MATT.95Nov10115356@cyclops5.berkeley.edu>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <199511091835.NAA11661@starling.WPI.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: matt@cyclops5.berkeley.edu (Matt Austern)
Newsgroups: comp.std.c++
Subject: Re: Virtual functions and destructors
Date: 10 Nov 1995 19:58:40 GMT
Organization: University of California at Berkeley (computational neuroscience)
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <MATT.95Nov10115356@cyclops5.berkeley.edu>
References: <199511091835.NAA11661@starling.WPI.EDU>
Reply-To: matt@physics.berkeley.edu
NNTP-Posting-Host: physics2.berkeley.edu