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", ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access