So, here is an example of a class I'm creating :
typedef struct st{
int counter;
int fields[128];
}stEx;
class Foo {
stEx *E;
int index;
public :
Foo(){
this->index = 0;
this->E = new stEx;
}
~Foo(){
delete E;
}
}
Since I want E to be the instance of the Foo object alone, the E object has to be destroyed automatically when the Foo object gets destroyed, and should'nt therefore outlive that object. That's how I came across the notion of smart pointers, and unique pointers in particular.
However, I can't seem to understand why I need to use unique pointers. And how do I destroy/free a unique pointer?
Here is my attempt with unique pointers.
#include <memory>
typedef struct st{
int counter;
int fields[128];
}stEx;
class Foo {
std::unique_ptr<stEx> E;
int index;
public :
Foo(){
this->index = 0;
this->E = std::unique_ptr<stEx>(new stEx());
}
~Foo(){
E.release; // ?
}
}
Thanks in advance!