Do not use pointers when you don't need them, and don't use owning raw pointers (unless you have a very good reason for them).
Use automatic storage duration:
std::list<char> stuff;
thing object{stuff};
The constructor of thing
would take std::list<char>
as its argument:
#include <utility> // for std::move
class thing {
public:
explicit thing(std::list<char> stuff_) : stuff(std::move(stuff_)) {}
private:
std::list<char> stuff;
};
If you do it this way, the destructor of thing
will be called when thing
goes out of scope, implicitly calling the destructor of stuff
. Many good C++ books explain this in great detail.
Unlike Objective-C, and C++ uses RAII rather than reference counting. The basic rule is: use automatic storage duration when possible, avoid raw owning pointers, and don't use new
unless you have a good reason for it.