Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!ucsd!pacbell.com!pacbell!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: value of 'this' in constructor Message-ID: <11206@alice.UUCP> Date: 17 Aug 90 13:08:19 GMT References: <12680@hydra.gatech.EDU> Distribution: na Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 34 In article <12680@hydra.gatech.EDU>, gt3930b@prism.gatech.EDU (NICHOLS,STEVEN MALCOLM) writes: > Consider the following piece of code. From p. 284 of "Using > C++", and also p. 164 of "The C++ Programming Language", I am lead to > believe that the value of the pointer 'this', on entry to a constructor, > will be 0 if the instance of the class has been dynamically allocated. If you are using the now-deprecated feature of assigning to `this,' and you are using a compiler that supports the feature, then `this' will be 0 on entry to your constructor -- BUT ONLY IF THE CONSTRUCTOR ASSIGNS TO `this' (believe it or not!). That is, the presence of an assignment to `this' in a constructor makes that constructor magic: not only is `this' then zero on entry to the constructor but you are obliged to assign some value to it on *every* path through the constructor. The following is typical: Foo::Foo() { if (this == 0) { this = /* some appropriate pointer */ } else { this = this; // believe it or not } // now you can deal with members of the object } The idea is that the act of assigning to `this' triggers initialization of base classes, and so on. You see why the feature is deprecated? -- --Andrew Koenig ark@europa.att.com