Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12036
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: only call dtor of derived class? |
| Date | 2011-12-03 00:32 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <9jt913FpolU1@mid.uni-berlin.de> (permalink) |
| References | <cu5id7l8bgtqdmm8udluq4batgujd09cpd@4ax.com> |
Heck <heckja_in@ordertostymieharvestersverizon.net> wrote:
> Can it be (easily) arranged to call only the destructor of a derived
> class, and call none of the destructors in the classes from which it
> derives? This may seem to seek to negate the simple and compelling
> value of the destructor tree, so, perhaps my design is flawed, but,
> perhaps not.
> I wrote the classes I'm wrestling with to handle memory-mapped files.
> The files that already exist require different sharing and access
> rights and different attributes than the files that are to be created
> and the files that are to be created are to be resized upon completion
> (extra bytes removed).
> Rather than pass in a small slew of args to effect this, I just wrote
> the sequence of calls twice, each set appropriately parameterized.
> With respect to the constructors, the derived classes happen to
> require parameters the base class does not, so, with that I can call
> the constructors appropriately, but as far as the destructors, it's
> either not allowed or it is and I don't know how to control which
> class gets which series of destructors called for it.
> I've pointers in the base class that I want to use in the derived
> classes.
> Simple tests in the base class' destructor will suffice to restrict
> the execution to the approriate objects, but, is there a way C++'s
> semantics of inheritance facilitates such a scenario?
As Victor has put it short and bluntly you're on the wrong
track. Not only can't that be done but you're violating one
of the most basic principles of OOP, i.e. that inheritance
is all about an "is-a" relationship. If for the derived class
the destructor of the base class can't be called for what-
ever reasons then the derived class isn't an object of the
type that is a specialization of the type of object the base
class is for. Even when you have to check in the base class
what actual kind of oject it's dealing with (as you seem to
consider as a "fix") you're on the wrong path.
A base class must never, ever have any idea what it's derived
classes do look like whatsoever. If it does it's already too
specialized for being a base class you can derive all the
types you want to from.
What you seen to be having here is a case like a base class
for vehicles where you started your design with the assump-
tion that all vehicles have an engine that can be switched
on and off. Thus you may have something to switch off thed
engine in the destructor of the "Vehicle" class. And that
works quite nicely for cars, motorcycles, lorries, most
airplanes etc. But then you realize that you also want to
have bicycles and gliders - and suddenly everything seems
to break down. You now ask if there's a way to avoid having
the destructor getting called that switches off the (non-
existent) engine. But the problem is that your base class
is simply too specialized.
Instead of "repairing" things in the base class by testing
if the vehicle has an engine and make it behave accordingly
(thus forcing the base class to be "aware" of what additional
properties its descendants can have) create a new, more
general class that doesn't know about the existence of an
engine and create two intermediate base classes, both in-
heriting from the original base class, that are for vehicles
with and without an engine. Now make your already existing
derived classes derive from the new intermediate class for
"vehicle with an engine" while deriving bicycles and gliders
from the new internediate class for "vehicle without an
engine".
The way you describe your problem it looks as if you started
with the assumption that memory-mapped files are all the same,
so having a single base class for them looked reasonable. But
now you've recognized that the property of just being "memory-
mapped" isn't what they all have in common. So revise your base
class to contain only what's common to all of them and create
intermediate classes you can derive from the more specialized
cases where there aren't any issues when all the destructors
are called.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
only call dtor of derived class? Heck <heckja_in@ordertostymieharvestersverizon.net> - 2011-12-02 13:50 -0500
Re: only call dtor of derived class? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-12-02 16:50 -0500
Re: only call dtor of derived class? Heck <heckja_in@ordertostymieharvestersverizon.net> - 2011-12-03 22:01 -0500
Re: only call dtor of derived class? jt@toerring.de (Jens Thoms Toerring) - 2011-12-03 00:32 +0000
Re: only call dtor of derived class? Heck <heckja_in@ordertostymieharvestersverizon.net> - 2011-12-03 22:07 -0500
Re: only call dtor of derived class? Heck <heckja_in@ordertostymieharvestersverizon.net> - 2011-12-04 18:08 -0500
Re: only call dtor of derived class? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-12-04 22:07 -0500
Re: only call dtor of derived class? Stuart Redmann <DerTopper@web.de> - 2011-12-04 23:36 -0800
Re: only call dtor of derived class? Werner <werasm@gmail.com> - 2011-12-05 00:53 -0800
Re: only call dtor of derived class? Stuart Redmann <DerTopper@web.de> - 2011-12-05 04:33 -0800
Re: only call dtor of derived class? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-12-11 18:47 -0500
Re: only call dtor of derived class? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-12-11 18:26 -0500
Re: only call dtor of derived class? Juha Nieminen <nospam@thanks.invalid> - 2011-12-05 13:00 +0000
Re: only call dtor of derived class? Goran <goran.pusic@gmail.com> - 2011-12-05 05:12 -0800
Re: only call dtor of derived class? Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-12-11 17:02 -0500
csiph-web