Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!rice!uw-beaver!apollo!mrst!sdti!wmm From: wmm@sdti.com (William M. Miller) Newsgroups: comp.lang.c++ Subject: Re: calling constructor (again) Message-ID: <1989Oct23.014006.18925@sdti.com> Date: 23 Oct 89 01:40:00 GMT References: <1989Oct13.175853.24335@polyslo.CalPoly.EDU> Reply-To: wmm@sdti.SDTI.COM (William M. Miller) Distribution: usa Organization: Software Development Technologies, Inc. Lines: 33 In article <1989Oct13.175853.24335@polyslo.CalPoly.EDU> ttwang@polyslo.CalPoly.EDU (Thomas Wang) writes: >I still have not solved the problem of directly calling the constructor. > >This requirement came from my garbage collection project. For each garbage >collectable class 'foo', there is a reference class 'ref_foo'. When a 'foo' >object is created, it must be registered with its reference handle. > >If I do the registration after the construction of 'foo', I will have a >problem. It is possible to trigger garbage inside the 'foo' constructor, when >the new object is not yet registered. There are some complications, but the >end result is that live objects will be deleted. > >The proper sequence must be: create space, register with reference, call >object constructor. > >If I cannot call constructors directly with AT&T compiler, >I will be forced to only implement for g++. The ability to overload operator new() on a per-class basis in 2.0 is exactly what you need here, rather than calling constructors directly. All you need to do is define your operator new() to do the first two steps (creating the space and registering the reference). Since operator new() is called before the constructor, the correct order is guaranteed, and the normal mechanism of constructor invocation need not be tampered with. And, since operator new() can be inherited, you can provide the necessary functionality in the base garbage collectable class and not worry about it for derived classes. -- Non-disclaimer: My boss and I always see eye-to-eye (every time I look in the mirror). wmm@sdti.sdti.com