Path: utzoo!attcan!uunet!decwrl!sdd.hp.com!ucsd!rutgers!mcnc!rti!dg-rtp!farm!taylor From: taylor@farm.rtp.dg.com (William Taylor) Newsgroups: comp.lang.c++ Subject: calling a constructor Keywords: shared memory Message-ID: <1990Aug1.145814.16140@dg-rtp.dg.com> Date: 1 Aug 90 14:58:14 GMT Sender: usenet@dg-rtp.dg.com (Usenet Administration) Reply-To: taylor@dg-rtp.dg.com Organization: Data General Corp., Research Triangle Park, NC Lines: 60 I would like to call a constructor directly on a piece of memory that I have allocated. I have a pointer to some memory (malloc'ed, shared memory, ...) and I would like to put a class there. How can I call the constructor for that class to initialize the memory? If I try the obvious: -- class foo { public: foo(); } *foobar_p; foobar_p = (foo *)malloc(sizeof(foo)); foobar_p->foo(); -- g++ 1.37.2.1 will not complain but calls foo() with "this" set to something other than "foobar_p". (It seems to be a temporary class on the stack). cfront will not let me do this. Instead I must make a new member function, "init()" to call the constructor. -- foo::init() { foo(); } -- However, this does not work either. The construct "foo()" means to allocate an object of type "foo" and return it as the value of the expression. Not what I want. One method that works is to have "init()" pass "this" to the constructor "foo()" as an argument and have "foo()" set "this" to the argument. foo::init() { foo(this); } foo::foo(foo *ptr) { this = ptr; } But, this is pretty ugly. Any other ideas on how to call a constructor for some piece of memory. (We are using shared memory.) Thanks in advance, William Taylor taylor@dg-rtp.dg.com Data General Corporation {world}!mcnc!rti!dg-rtp!taylor Research Triangle Park, NC 27709 (919) 248-5801