Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!apple!bgibbons From: bgibbons@Apple.COM (Bill Gibbons) Newsgroups: comp.lang.c++ Subject: Re: calling constructor (again) Message-ID: <35628@apple.Apple.COM> Date: 13 Oct 89 22:34:09 GMT References: <1989Oct13.175853.24335@polyslo.CalPoly.EDU> Distribution: usa Organization: Apple Computer Inc, Cupertino, CA Lines: 41 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. >... >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++. You can't explicitly call a constructor, but you can overload the NEW operator and give it an extra argument. If that argument is a pointer (the original reason for allowing this), the overloaded NEW can just return its argument. Naturally, the constructor is invoked next and you get the effect you wanted. See section 5.3.3 of the AT&T reference manual for CFront 2.0 . For example: class T { public: T(); void *operator new(unsigned int, void *where) { return where; } void operator delete(void*); }; void f() { void *p; T *t = new (p) T; } You can also explicitly call the destructor, as in: t->T::~T(); Note that the new and delete functions must be public. As with other overloaded functions, multiple flavors of new and delete can be declared for a given class. Bill Gibbons bgibbons@apple.com