From -6536656620649605495
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8466591b9cd11017
X-Google-Attributes: gidf78e5,public
From: Christopher Smith <ccas@pacbell.net>
Subject: Re: C++ 'philosophy' question, Constructors/Destructors
Date: 2000/07/18
Message-ID: <igVb5.441$bL1.123705@news.pacbell.net>#1/1
X-Deja-AN: 647349890
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <394a5536.29331042@news.columbia.edu> <newscache$omc9wf$h71$1@firewall.thermoteknix.co.uk>
X-Priority: 3
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 963845697 mail2news:11687 mail2news mail2news.demon.co.uk
Organization: SBC Internet Services
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-MSMail-Priority: Normal
NNTP-Posting-Date: Sat, 15 Jul 2000 01:27:58 PDT
Newsgroups: comp.std.c++


Ken Hagan <K.Hagan@thermoteknix.co.uk> wrote in message
news:newscache$omc9wf$h71$1@firewall.thermoteknix.co.uk...
> "Leonid Portnoy" <lp178@columbia.edu> wrote in message
> news:394a5536.29331042@news.columbia.edu...
> >
> <snipped a lot of stuff on wanting 2 dtors...>
>> 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...

>The second constructor and destructor are only needed if someone
>goes on to write the derived class. I have no objection to writing
>a class with a view to deriving certain related classes from it, but
>if I do this too much, I lose encapsulation. (The needs of the
>derived classes are now driving the design of the base class.)

And this second constructor does not fully construct the object -- it is
broken as the
memory doesnt point to anything!

Another problem is that absent a "assign destructor" statement of some kind
the only logical
counterpart to 1 destructor is to have 1 per constructor. And this makes the
normal case of many
constructors all going to the same internal state require tons of identical
code be maintained. Ick.

And if you really want to make code that changes the behavoir of the base
class variables, while not nice
you can just change the base class to allow you to do this with some kind of
flag variable. Of course, the best
solution if the needs of the class have changed such that memory mgmt of the
buffer should not be in the base is
to create a manager class family derived from a common base. Then you can
use virtual functions and inheritance to
have different types of buffers be handled 'the same way' from our class'
point of view but correctly, too.


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




