delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/20/18:15:21

From: bjorn AT algonet DOT se (Bjorn Fahller)
Newsgroups: comp.os.os2.programmer.misc,gnu.gcc.help,comp.os.msdos.djgpp
Subject: Re: C++ problem -- Over 30 people were unable to help me, can you?!
Date: 20 Jan 1998 23:00:53 GMT
Organization: Algonet/Tninet
Lines: 44
Message-ID: <DlpnOhDvP3gv-pn2-35mmE98EdfGH@du125-5.ppp.algonet.se>
References: <bWLoegW7sFse-pn2-d679pGp6mnys AT portD23 DOT Generation DOT NET> <j5l67ng499w DOT fsf AT tcadcs6 DOT sc DOT intel DOT com>
NNTP-Posting-Host: du125-5.ppp.algonet.se
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019