I have need of an object level variable (TIniFile* ini
). In the past, the code samples and/or convention would be new
/delete
the object, like this:
frmMain.h
...
private:
TIniFile* ini;
...
constructor
...
ini = new TiniFile(fileName);
...
destructor
...
delete ini;
...
I have been seeing alot about using std::unique_ptr()
instead. Does this do what I think it does: automatically release memory when the object is released?
frmMain.h
#include <memory>
...
private:
std:unique_ptr<TIniFile> ini;
...
constructor
...
ini = make_unique<TiniFile>(fileName);
...
no destructor
Does this do what I think it does: automatically release memory when the object is released?
Yes. When the containing object is destroyed, it will destroy its unique_ptr
member object, which in turn will destroy the TIniFile
object it owns.