The use of operator "new" ?

Started by
1 comment, last by initial_y 17 years, 8 months ago
I saw some code in a template class :
::new (&m_pData[nIndex]) TYPE ;
is this mean :
&m_pData[nIndex] = ::new TYPE ;
汇编语言不会编
Advertisement
No, it's not quite the same. It is the so-called placement new. Basically you provide the new operator with an already allocated memory block and tell it to call an objects constructor at that position.

That's useful for object pools. The container allocates the memory for 100 objects in advance (in one single block) without ever constructing an object. Now when you request a new object the container simply calls the placement new which constructs an object at a specific position.

Note that objects constructed this way may not be deleted by calling delete. They have to specifically be only destructed:

pObject->~CObject();

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

thanks, I am clear now :)
汇编语言不会编

This topic is closed to new replies.

Advertisement