Reputation: 1202
What this code means? As per this link there is no way to pass pointer to the new operator. the new
operator here is default one, not an overloaded one. please help
LPVOID m_Buffer;
MyClass* mc = new(reinterpret_cast<void*>(m_Buffer)) MyClass;
Upvotes: 1
Views: 857
Reputation: 25927
This is a placement new. This version of new operator allows you to place new instances in pre-allocated memory. It is quite useful when you want to allocate a lot instances, because you'll drastically reduce count of actual memory operations, which may increase performance of your application.
Example (taken from Parashift C++ FAQ):
#include <new> // Must #include this to use "placement new"
#include "Fred.h" // Declaration of class Fred
void someCode()
{
char memory[sizeof(Fred)]; // Line #1
void* place = memory; // Line #2
Fred* f = new(place) Fred(); // Line #3 (see "DANGER" below)
// The pointers f and place will be equal
...
}
It is worth noting though, that you have to know exactly how much space a class needs and that may vary due to compiler settings/platform and a dozen other reasons. So use it only if you need it and when you really know what you're doing.
Upvotes: 1
Reputation: 8164
This is a placement new. It is used to e.g. place objects at an specified location.
Upvotes: 5