Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!ukc!dcl-cs!aber-cs!odin!pcg From: pcg@cs.aber.ac.uk (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: calling a constructor Message-ID: Date: 3 Aug 90 15:36:03 GMT References: <1990Aug1.145814.16140@dg-rtp.dg.com> Sender: pcg@aber-cs.UUCP Organization: Coleg Prifysgol Cymru Lines: 38 In-reply-to: taylor@farm.rtp.dg.com's message of 1 Aug 90 14:58:14 GMT "taylor" == William Taylor writes: taylor> I would like to call a constructor directly on a piece of memory taylor> that I have allocated. taylor> I have a pointer to some memory (malloc'ed, shared memory, ...) taylor> and I would like to put a class there. How can I call the constructor taylor> for that class to initialize the memory? What about overloading operator new ? In the extreme you can have an operator new that does not actually do any allocation, just takes as an extra argument the address of the already allocated area. The compiler will arrange for the constructor to be called after operator new is done. taylor> [ ... a lot of ugly ideas examined and discarded ... ] taylor> But, this is pretty ugly. Any other ideas on how to call a constructor taylor> for some piece of memory. (We are using shared memory.) This is really the job for which operator new overloading was designed, which is the generalization of the mechanism used to obtain the effect of the "placement syntax" available in older versions. Cavat: remember tha if you are using GNU C++ its syntax for extra parameters to operator new requires braces, not parenthesis, around them. Note: incidentally all the difficulties you had in invoking a costructor by itself are because a constructor is really a member function of the class object, and there is no class object in C++. -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk