[comp.lang.c++] calling a constructor

taylor@farm.rtp.dg.com (William Taylor) (08/01/90)

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

pcg@cs.aber.ac.uk (Piercarlo Grandi) (08/03/90)

"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

rpk@wheaties.ai.mit.edu (Robert Krajewski) (08/06/90)

I don't really fancy myself a C++ expert, but doesn't the standard
C++ library provide an alternate (overloaded) form of new:

void *operator new ( long size, void *memAddress );

that effectively invokes the constructor properly ?  (This is found in
the _C++ Primer_, page 164, but should the long there actually be a
size_t ?)

Suppose IntArray is a class with a constructor that takes an int for
an argument:

#include <new.h>

char buf [ sizeof(IntArray) ];

// Normal new use
IntArray *pa = new IntArray(10);

// Give new a place
IntArray *pbuf = new (buf) IntArray( 10 );

-- 
Robert P. Krajewski
Internet: rpk@ai.mit.edu ; Lotus: robert_krajewski.lotus@crd.dnet.lotus.com