views:

49

answers:

2

I have a fictional class:

template<typename T> class demonstration
{  
    public:
    demonstration(){}
    ...
    T *m_data;
}

At some point in the program's execution, I want to set m_data to a big block of allocated memory and construct an object T there.

At the moment, I've been using this code:

void construct()
{
    *m_data = T();
}

Which I've now realised is probably not the best idea... wont work under certain cirumstances, if T has a private assignment operator for example.

Is there a normal/better way to do what I'm attempting here?

+8  A: 

Use placement new:

new (m_data) T();

Placement new is really just an overload of the operator new function that accepts an additional parameter – the memory location where the object should be constructed at. This precisely matches your use-case.

In particular, this is how allocators usually implement the construct method which is used (among others) by the STL container classes to construct objects.

Since placement new only constructs an object without allocating memory, it’s usually an error to call delete to get rid of the memory. Destruction has to happen by calling the destructor directly, without freeing the memory:

m_data->~T();

Notice that this syntax for calling the destructor doesn’t work for a constructor call, otherwise we wouldn’t need placement new in the first place. I.e. there’s no m_data->T().

Konrad Rudolph
So placement new is simply used to construct an object at a location, as I need. Does it go with a placement delete? Or in this case, can new stand alone?
SalamiArmi
@SalamiArmi: You don't use any "placement delete". Instead, you need to manually call the destructor when you're done: `m_data->~T()`. This is because `delete` deallocates memory. You just want to get rid of the `T` there. Later you'll have to free the memory `m_data` points to, somehow.
rlbond
Thanks for clearing that up, rlbond. Was just curious if there was some odd behaviour involved.Thank you for your answers!
SalamiArmi
@SalamiArmi: Well spotted, I forgot to mention that. I’ve updated my answer accordingly. But @rlbond has already given the correct answer.
Konrad Rudolph
+1  A: 

Placement new operator is what would suit your situation.

Kotti