From -6837064584789105554
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,8466591b9cd11017
X-Google-Attributes: gidf78e5,public
From: Robert Klemme <robert.klemme@myview.de>
Subject: Re: C++ 'philosophy' question, Constructors/Destructors
Date: 2000/06/17
Message-ID: <394A6A5B.EC5FA66E@myview.de>#1/1
X-Deja-AN: 635425420
Content-Transfer-Encoding: quoted-printable
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <394a5536.29331042@news.columbia.edu>
X-Accept-Language: de
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 961178427 mail2news:22076 mail2news mail2news.demon.co.uk
Organization: myview technologies
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Mime-Version: 1.0
NNTP-Posting-Date: 16 Jun 2000 17:56:49 GMT
X-MIME-Autoconverted: from 8bit to quoted-printable by mulga.cs.mu.OZ.AU id EAA27631
Newsgroups: comp.std.c++



Leonid Portnoy schrieb:
>=20
> Hi,
>=20
>     What is the reason C++ automatically calls the destructor for the
> base class after calling the destructor for the derived class?
>=20
>     Sometimes this is not needed, and can even be harmful.

in 99.9999% of all cases it is harmful if a base class destructor is NOT
called automatically.  this is one of the most important features of oo
languages in general and c++ in particular.

>      However, now when I destroy this derived class, I  want to
> release the memory (again by using directX api), and to return. But
> instead, after this C++ calls the base class's destructor, which
> attempts to free the data as if it was regular memory. Obviously this
> causes problems.

yes, but your problems started with the constructor: if the base class
is responsible for allocation then it is responsible for deallocation,
too.  if it just stores the pointer but relies on derived classes to
properly initialize the memory, then it should neither allocate it nor
free it.  this is symmetrical.

>      Of course, I can set data=3DNULL, set flags, etc.. to prevent the
> base class's destructor from doing damage, but this seems like an
> inelegant solution.

no, i do not think so.

did you think that there might be a design problem?  you might have to
separate memory mangement of your bitmap from the functionality.  this
is the solution that seems to me the most straightforward.  you'd then
could have an abstract base class with methods for access to he memory;
you'd then derive a class for each memory model you choose and then you
created another class that uses you abstract base class as interface and
contained the algorithms.  or you would have an abstract base class with
all the algorihtms and abstract methods for memory management.  then you
would derive two classes - one for each memory model.  or you would
choose the approach stl containers that are parametrized by an allocator
class or...

>      This raises another question : Why does a class have only one
> destructor, but multiple constructors? Why not have multiple
> destructors, that will be called based on what constructor was used to
> create the object.

this is not normally necessary because the process of resource
deallocation is always the same.  if you have to distinguish these then
you can remember the way of construction yourself and treat it
appropriately.

and: how would you guarantee, that the number of destructors matches the
number of constructors?  what, if they are different?

regards

	robert

--=20
Robert Klemme
Software Engineer
-------------------------------------------------------------
myview technologies GmbH & Co. KG
Riemekestra=DFe 160 ~ D-33106 Paderborn ~ Germany
E-Mail: mailto:robert.klemme@myview.de
Telefon: +49/5251/69090-321 ~ Fax: +49/5251/69090-399
-------------------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




