[net.lang.c++] Initializing objects on the heap

sher@rochester.UUCP (05/01/86)

Right now I am writing in c++ a program to manipulate a linked list.

This means that proceedures and function often want to create objects
on the heap since it is a bad idea to return objects on the stack.
Also it is a good idea to initialize all objects you create.

The only way I know to put an initialized object onto the heap is:

foo * fp = new foo;
foo f ( init );
*fp = init;

If f is a large object with many pieces this is a very wasteful proceedure
since this involves copying f into the space of fp after going to the
work of creating and allocating f.  Is there a better way of doing
this?  I know it can be done by creating a function that takes
fp and initializes it with init but I find this inelegant.  Why have
constructors if they have to always be bypassed?

-- 
-David Sher
sher@rochester
seismo!rochester!sher

ark@alice.UucP (Andrew Koenig) (05/02/86)

> The only way I know to put an initialized object onto the heap is:

> foo * fp = new foo;
> foo f ( init );
> *fp = init;

Can't you say:

	foo * fp = new foo (init);

?