ROBERT RICHARDSON
ROBERT RICHARDSON

Reputation: 2299

How does the behavior of this override of placement new change things?

I just came across a usage of placement new that I don't understand, which I suppose is not surprising since I've never used it at all.

// Copy event type and map data reference from  queue entry *pData to its
// local copy via placement new.
// Note: Can't use a setter nor assignment operator here because
// Event Data contains a reference member variable.
Data eventData;
new (&eventData) Io::Event::Data(*pData);

The Data class contains this:

////////////////////////////////////////////////////////////////////////////
//  METHOD NAME: Io::Event::Data::*operator new
//
/// Placement new operator
////////////////////////////////////////////////////////////////////////////
void *operator new(size_t size UNUSED, void *pMem) { return pMem; };

What is the effect of the overridden placement new operator? If it did not exist, then the class's copy constructor would be invoked. But what does this do? I did find an example of an override looking exactly like this elsewhere on SO, but it didn't explain it. Does the use of this override merely copy the bytes from the source location into the target location?

Upvotes: 0

Views: 52

Answers (0)

Related Questions