create an object into a specific address

R

Ramesh Tharma

Hi Guys,

How do you create an object into a specific address ?

Thanks,
Ramesh
 
I

Ivan Vecerina

Ramesh Tharma said:
How do you create an object into a specific address ?

With the built-in placement-new:
#include <new>
then:
MyClass *p = new ( (void*)theAddress ) MyClass(constructorParams...);

You will then also be in charge of destroying the object,
as follows:
p->~MyClass();

Keep in mind this is an advanced technique (difficult to get right).
Beware of memory-alignment requirements that the class may have,
and of proper exception-handling...


You will find uses of these technique in the implementation
of standard library containers, which you should have access to.


hth -Ivan
 
K

Kristo

Phil said:
Look up "new with placement."

Please quote an appropriate amount of the original post when replying.
Not everyone reads their news in a threaded form.

BTW, placement new is in the FAQ. See section 11.10.

Kristo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top