Say that I have a class A
and a class B
which further has a subclass B1
. Now I want to assign a pointer of B1
to be a member of A
. I could do that by simply heap allocating B1
and then using delete
in the destructor of A
as follows:
class B {
public:
B() { };
};
class B1 : public B {
public:
B1() { };
};
class A {
public:
B* b_instance;
A(B* b_instance){
this->b_instance = b_instance;
};
~A(){
delete this->b_instance;
};
};
int main() {
B* b_instance = new B1();
A a_instance(b_instance);
return 0;
};
Now I believe another approach (and one that is more favored?) would use std::unique_ptr
, whereby a unique_ptr
to B1
is given to A
and thus, as a smart pointer, whenever A
is destroyed B1
is automatically destroyed as well since there are no more references to it. My best guess is that it should look something like this:
#include <memory>
class B {
public:
B() { };
};
class B1 : public B {
public:
B1() { };
};
class A {
public:
std::unique_ptr<B> b_instance;
A(B& b_instance){
this->b_instance = std::make_unique<B>(b_instance);
};
~A(){ };
};
int main() {
B1 b_instance;
A a_instance(b_instance);
return 0;
};
Is the unique_ptr
approach the same as the approach outlined above so long as you remember to include delete this->b_instance
in the destructor of A
? Or are there other benefits of the unique_ptr
approach?
Is the
unique_ptr
approach the same as the approach outlined above so long as you remember to includedelete this->b_instance
in the destructor ofA
?
More or less.
Or are there other benefits of the
unique_ptr
approach?
Yes, you get automatic move semantics and copy semantics is turned off, greatly helping to prevent involuntary multiple owners of the same resource.