Std::unique_ptr vs. instantiated member variable

Sorry, I may not have the terminology correct, but what’s the practical difference between declaring something like this (just an example):

std::unique_ptr<AudioProcessorGraph> mainProcessor;
mainProcessor.reset(new AudioProcessorGraph);

// (Or passing it in during a constructor:)
class foo (AudioProcessorGraph& graph) : mainProcessor (graph)

And just instantiating it as a variable?:

AudioProcessorGraph mainProcessor;

With the first you use a ptr to access vs. a dot to access:


… but they both seem to work. I’ve seen it both ways, and unless you’re passing some special parameters during construction, I wonder if there is any practical difference?

If you want to create, switch or delete the used instance dynamically, you have to use a (smart) pointer. (AudioProcessorGraph can’t be copied or assigned to.)

In some cases instantiating objects directly as variables may be problematic because they can consume too much stack space. (However if the objects live as members of an object that is itself heap allocated, those sub-objects will be on the heap too.)

You might also choose to use pointers if you want to speed up your incremental build times using the pimpl idiom.