Skip to main content
GameDev.net gamedev.net
🔒 Locked

The use of operator "new" ?

Started by initial_y Jul 28, 2006 at 11:55 PM 1 replies 600+ views
Original Post
initial_y
initial_y
I saw some code in a template class :
::new (&m_pData[nIndex]) TYPE ;
is this mean :
&m_pData[nIndex] = ::new TYPE ;
汇编语言不会编
Endurion
Endurion
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>
initial_y
initial_y
thanks, I am clear now :)
汇编语言不会编

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.