Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!uw-beaver!apollo!mrst!sdti!wmm From: wmm@sdti.SDTI.COM (William M. Miller) Newsgroups: comp.lang.c++ Subject: Re: Constructors and new Message-ID: <458@sdti.SDTI.COM> Date: 2 Jun 89 12:14:00 GMT References: <2684@ssc-vax.UUCP> Reply-To: wmm@sdti.UUCP (0006-William M. Miller) Organization: Software Development Technologies, Sudbury MA Lines: 16 In article <2684@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: > According to BS, section 5.5.7, a constructor for a class can determine whether >it was called by new or not. That only works if the constructor assigns to `this.' (That's the context of the statement in 5.5.7 you mention, although the connection is not as clear as it ought to be.) What happens is that, if there is an assignment to `this' in the body of the constructor, the value of `this' is left as it is upon entry until that assignment occurs: zero if allocated via `new,' nonzero for static and auto. If there is no assignment to `this,' the compiler inserts code at the beginning of the constructor, before any of the statements in the body, to check if `this' is zero and do the allocation via operator new() if needed. The allocation and assignment to `this' have already occurred before the test in your example constructor. This question illustrates the reason assignment to `this' is replaced in version 2.0: it's confusing, unclear, error-prone, etc.