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: References: NNTP-Posting-Host: du125-5.ppp.algonet.se Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Mon, 19 Jan 1998 18:18:51, Claus Fischer 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.