From 6889032843566173305
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8466591b9cd11017,start
X-Google-Attributes: gidf78e5,public
From: lp178@columbia.edu (Leonid Portnoy)
Subject: C++ 'philosophy' question, Constructors/Destructors
Date: 2000/06/17
Message-ID: <394a5536.29331042@news.columbia.edu>#1/1
X-Deja-AN: 635392763
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Authentication-Warning: backdraft.briar.org: smap set sender to <news@columbia.edu> using -f
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 961173150 mail2news:18358 mail2news mail2news.demon.co.uk
Organization: Columbia University
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
NNTP-Posting-Date: 16 Jun 2000 16:27:30 GMT
Newsgroups: comp.std.c++


Hi,

    What is the reason C++ automatically calls the destructor for the
base class after calling the destructor for the derived class? 
 
    Sometimes this is not needed, and can even be harmful. For
example, I have a Bitmap class, that does operations on graphical
data. It has an internal pointer to the graphics : 'void *data' as a
member variable, which gets initialized (memory allocated for it) upon
construction, and deallocated in the destructor.

    Now, I wanted to write a DirectX bitmap class, which would perform
the same graphical operations on data, except that the data would be
stored in memory provided by DirectX. So it made sense to derive from
the Bitmap class, and in the constructor obtain a pointer from DirectX
API, and set 'data' to it.

     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. 

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

     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.

If there could be many destructors, I would specify, in my base Bitmap
class, a second destructor which would not attempt to free memory.
Let's say it would be called if constructor #2 of the base class was
used to create the object. This constructor would not allocate any
memory but just initialize other members.

Then in the derived class's constructor, I would specify how to create
the base class (by calling constr. #2), and then when destroying my
derived class it would automatically call the appropriate destructor
(the one that doesn't free memory) of the base class...

Any thoughts?
							Leonid Portnoy

---
[ 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              ]




