I am implementing the smart pointer and trying to use it vector of pointers so that I don't have to delete the pointers once it goes out of scope. Below is the program that I wrote but it is crashing. Can someone please help me with this?
#include<iostream>
#include<vector>
using namespace std;
template<class T>
class smrtP
{
T *p;
public:
smrtP(T* pp = NULL):p(pp) {}
~smrtP() { delete p;}
T& operator*() {return *p;}
T* operator->() {return p;}
};
class B
{
public:
virtual void draw() = 0;
virtual ~B() { cout<<"~B"<<endl; }
};
class D:public B
{
public:
D() { cout<<"D"<<endl;}
virtual void draw()
{
cout<<"Draw D"<<endl;
}
virtual ~D() { cout<<"~D"<<endl; }
};
int main()
{
typedef smrtP<B> sp;
vector<sp> v;
sp ptr(new D());
v.push_back(ptr);
}