I'm quite new to smart pointers and I encountered a problem with the code below
#include <memory>
#include <vector>
using namespace std;
class A
{
public:
int var;
};
class B : public A
{
public:
vector<unique_ptr<int>> vec;
};
int main()
{
{
unique_ptr<B> b(new B);
b->vec.push_back(unique_ptr<int>(new int(4)));
unique_ptr<A> a = unique_ptr<A>(move(b));
}
system("pause");
return 0;
}
In this example the 4 added to vector vec is not deleted when pointer a goes out of scope, but when I change it so that it doesn't cast to base class then all resources are returned as intended, how do I manage deleting variables defined in derived classes when using polymorphism and unique_ptr?