Mail Archives: djgpp/1998/01/20/18:15:21
On Mon, 19 Jan 1998 18:18:51, Claus Fischer
<cfischer AT tcadcs6 DOT sc DOT intel DOT 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.
- Raw text -