Path: utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!chalmers.se!appli!niklas From: niklas@appli.se (Niklas Hallqvist) Newsgroups: comp.lang.c++ Subject: Re: operator++() and testing 'this' Message-ID: <1163@appli.se> Date: 16 Oct 90 07:31:31 GMT References: <1990Oct14.224706.1934@nowhere.uucp> <58242@microsoft.UUCP> Organization: Applitron Datasystem AB, GOTHENBURG, SWEDEN Lines: 62 jimad@microsoft.UUCP (Jim ADCOCK) writes: >In article <1990Oct14.224706.1934@nowhere.uucp> sking@nowhere.uucp (Steven King) writes: >> >>In article <1990Oct9.144542.19983@sco.COM> jfischer@sco.COM (Jonathan A. Fischer) writes: >>>In article <706@msor0.UUCP> kt@msor.UUCP (Keith Tizzard) writes: >>>>Can one rely on 'this' having the value zero on entry to the constructor >>>>if the object is allocated on the free store? >>> >>> Yep, according to the same anachronisms section. >> >> And only if the object isnt a member (or base) of another object. >>Even tho' the translator knows this information (it does pass a flag to >>destructors) there is NOT a reliable mechanism to determine this from within >>a constructor. >Good point. Embedded objects are another good example why C++ compilers >DO NOT typically support the (this==0) test to see if objects are on the >heap or not. I haven't seen a C++ compiler released in years that >supports this test. Correct me if you find a counter-example. [ reasoning about why this == 0 not is supported deleted ] >To my mind, the right thing to do is to keep the language out of these >non-portable issues. Usually, programmers can arrange for some simple, >if non-portable test to see if an object is "on the stack" Well, as far as I can tell there IS a portable way testing this property. Please go on and flame me if my reasoning is invalid! Make a base class like this: class Locatable { private: static int aux_is_in_heap; // Needed because we can't set non-static // members before the ctor is called. int is_in_heap; public: Locatable() { is_in_heap = aux_is_in_heap; aux_is_in_heap = 0; } void* operator new(size_t sz) { aux_is_in_heap = 1; /* real "new" code */ } int in_heap() { return is_in_heap; } }; Locatable::aux_is_in_heap = 0; then go on deriving publicly from Locatable, you now have the method in_heap to play with. Be aware of member objects, they appear as if they are not on the heap, even if their enclosing object is, since new is only called for the container object. I would be interested in hearing about a portable function to differentiate stack objects & member objects residing on the heap. Of course, in a multi-tasking environment this scheme has to be refined, this is left as a trivial exercise to the reader :-). I would be very happy if you tell me about other flaws you see with this method. Niklas -- Niklas Hallqvist Phone: +46-(0)31-19 14 85 Applitron Datasystem Fax: +46-(0)31-19 80 89 N. Gubberogatan 30 Email: niklas@appli.se S-416 63 GOTEBORG, Sweden sunic!chalmers!appli!niklas