Article View: comp.os.os2.programmer.misc
Article #308Re: C++ problem -- Over 30 people were unable to help me, can you?!
From: Bjorn Fahller
Date: Tue, 20 Jan 1998 00:00
Date: Tue, 20 Jan 1998 00:00
44 lines
2311 bytes
2311 bytes
On Mon, 19 Jan 1998 18:18:51, Claus Fischer
<cfischer@tcadcs6.sc.intel.com> wrote:
> would fail for the same reason (ARM section 12.1). My ANSI draft
> standard of 1996 is unfortunately not very elaborate on the point of
> explicit constuctor calls; it seems that just not mentioning the
> ability to explicitely call a constructor bans its usage, and the fact
> that the syntax for the explicit constructor call is already used for
> type conversions means anytime you do something like the above it will
> result in creating a temporary, but not manipulating B.
I tried to look it up in the December 1997 final draft, but it's vague
too. 21.1/12 ends with "The address of a constructor should not be
taken." I don't know if calling one is taking its address (you could
argue both ways, I think.) However, there is one way in which an
explicit call can be legally made, and that is through placement new:
class A
{
A() {}
}
class B : A
{
B() { new ((A*)this) A();}
}
That is, construct an A on the address of the A ancestor of B.
It still is dangerous to do, at the minumum resources allocated by A's
constructor (the first time called) will not be released. A way around
this is to explicitly call A's destructor, which incidentally *is*
legal.
class B : A
{
B() { ((A*this))->~A(); new ((A*)this) A();}
}
I'm not sure if this yields undefined behaviour, but it is legal C++
(save for possible typos.)
_
/Bjorn.
Technical Details
Message-ID:
<DlpnOhDvP3gv-pn2-35mmE98EdfGH@du125-5.ppp.algonet.se>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<bWLoegW7sFse-pn2-d679pGp6mnys@portD23.Generation.NET> <j5l67ng499w.fsf@tcadcs6.sc.intel.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,8dafc9e0bf3f0357 X-Google-Attributes: gid11318c,public X-Google-Thread: 10891a,8dafc9e0bf3f0357 X-Google-Attributes: gid10891a,public X-Google-Thread: 11307a,8dafc9e0bf3f0357 X-Google-Attributes: gid11307a,public From: bjorn@algonet.se (Bjorn Fahller) Subject: Re: C++ problem -- Over 30 people were unable to help me, can you?! Date: 1998/01/20 Message-ID: <DlpnOhDvP3gv-pn2-35mmE98EdfGH@du125-5.ppp.algonet.se> X-Deja-AN: 317817061 Content-Transfer-Encoding: 8bit References: <bWLoegW7sFse-pn2-d679pGp6mnys@portD23.Generation.NET> <j5l67ng499w.fsf@tcadcs6.sc.intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Organization: Algonet/Tninet Newsgroups: comp.os.os2.programmer.misc,gnu.gcc.help,comp.os.msdos.djgpp