Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!munnari!otc!mikem From: mikem@otc.OZ (Michael Mowbray) Newsgroups: comp.lang.c++ Subject: Re: Address of constructor? Message-ID: <54@otc.OZ> Date: Tue, 9-Dec-86 03:36:51 EST Article-I.D.: otc.54 Posted: Tue Dec 9 03:36:51 1986 Date-Received: Tue, 9-Dec-86 11:14:33 EST References: <165700002@uiucdcsb> Organization: O.T.C. Systems Development, Australia Lines: 44 In article <165700002@uiucdcsb>, kenny@uiucdcsb.cs.uiuc.edu writes: > I discovered that I couldn't find a syntax that cfront would accept for > taking the address of a constructor. I want to do this because I am in > a situation where I have allocated memory by another means that I want > to initialize as an instance of a class, so I want to pass it to the > constructor as ``this''. Sounds contrary to C++ philosophy to me. There have been times when I would have liked to do this, but I eventually realised that it just reflected bad design. (Of course, I don't know the constraints applying in this particular case.) > The reason why is complicated; suffice it to say that I am > dealing with a situation where I can't rewrite the constructor and > have to use a memory allocator other than the standard _new operator. You could possibly do the following, but I'd try to re-structure the design in preference. class Given { // The class you're interested in. int i; public: Given(int ii); // - can't be re-written, say. }; struct Aux : public Given { // Auxiliary class Aux(void* buf, int ii) : (ii) // buf must have correct size & alignment { this = (this==0) ? (Aux*)buf : this; } }; main() { int buffer[sizeof(Given)/sizeof(int)]; // Or use space which the // non-standard allocator gave. Given *g = (Given*)(new Aux(buffer,1)); // we want a Given with i=1, say. } - not really recommended.