Earlier quoted context omitted.
The point of the example is that this is how NOT to do it in C++. There are two better options: void f(int n, int x) { Gadget p(n); // Stack allocated // ... if (x Or, if it really has to be a pointer: void f(int n, int x) { std::unique_ptr p = new Gadget(n); // Smart pointer // ... if (x Both will be automatically freed as soon as the scope is exited.
What I was hoping you'd say :) This is fine until I want to do this, at which point C++ becomes a memory management bastard: void f(int n, int x) { Gadget g = new Gadget(n); // ... if (x
In C++, you have various ways of referencing and passing objects, so you need to be aware of the lifetime and ownership of objects. It's arguably harder, and unfortunately, the C heritage makes it a lot harder than it needs to be :(