Looking Again at Placement new

Recall that placement new allows you to specify the memory location used to allocate memory. Chapter 9, “Memory Models and Namespaces,” discusses placement new in the context of built-in types. Using placement new with objects adds some new twists. Listing 12.8 uses placement new along with regular new to allocate memory for objects. It defines a class with a chatty constructor and destructor so that you can follow the history of objects.

Listing 12.8. placenew1.cpp

// placenew1.cpp  -- new, placement new, no delete#include <iostream>#include <string>#include <new>using namespace std;const int BUF = 512;class JustTesting{private:    string words;    int number;public:    JustTesting(const string & s = "Just Testing", ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.