Article View: comp.std.c++
Article #2095Re: object contruction/destruction in c++, design flaw?
From: Fergus Henderson
Date: Mon, 02 Oct 1995 15:11
Date: Mon, 02 Oct 1995 15:11
96 lines
2972 bytes
2972 bytes
henk_kampman@idsys.nl (Henk Kampman) writes:
>Why is the destructor only called for fully contructed objects?
If the destructor were called for partially constructed objects, how
would it know which parts of the object to destroy? This would require
the constructor first initialize all sub-objects in a way that could
not possibly throw any exceptions, and then later assign them their final
values; in many cases, this might not even be possible, and even in those
cases where it is possible, it may be inefficient.
>To my opinion this results in code duplication in the contructor and destructor
That need not be the case. For exampe, in a lot of cases you can use
`auto_ptr' to avoid code duplication.
>class a
>{
>public:
> char* mBuffer1;
> char* mBuffer2;
> a();
> ~a();
>};
Use the following instead:
class a {
public:
auto_ptr<char> mBuffer1;
auto_ptr<char> mBuffer2;
a();
~a();
}
>a::a()
>{
> mBuffer1 = NULL;
> mBuffer2 = NULL;
>
> try
> {
> mBuffer1 = new char[1000];
> if(mBuffer1 == NULL)
> throw MemError;
According to the draft standard, `new' will throw an exception rather
than returning a null pointer, so the `if' statement here is unnecessary.
> mBuffer2 = new char[1000];
> if(mBuffer1 == NULL)
> throw MemError;
> }
> catch(...) // allocation failed
> {
> delete mBuffer1;
> delete mBuffer2;
>
> throw;
> }
>
>}
With auto_ptr, this becomes just
a::a()
{
mBuffer1 = new char[1000];
mBuffer2 = new char[1000];
}
or alternately
a::a()
: mBuffer1(new char[1000]),
mBuffer2(new char[1000])
{}
>a::~a()
>{
> delete mBuffer1;
> delete mBuffer2;
>}
With auto_ptr, you don't even need a destructor - the compiler-generated
one will do the right thing.
--
Fergus Henderson | "Australia is the richest country in the world,
fjh@cs.mu.oz.au | according to a new system of measuring wealth
http://www.cs.mu.oz.au/~fjh | announced by the World Bank yesterday."
PGP: finger fjh@128.250.37.3 | - Melbourne newspaper "The Age", 18 Sept 1995.
[ 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:
<9527516.21208@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<henk_kampman-2909951146290001@huisie-ppp1.knoware.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Newsgroups: comp.std.c++ Subject: Re: object contruction/destruction in c++, design flaw? Date: 2 Oct 1995 15:11:00 GMT Organization: Computer Science, University of Melbourne, Australia Sender: news@cs.mu.OZ.AU (CS-Usenet) Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <9527516.21208@mulga.cs.mu.OZ.AU> References: <henk_kampman-2909951146290001@huisie-ppp1.knoware.nl> NNTP-Posting-Host: taumet.eng.sun.com Originator: clamage@taumet