# smart pointer implementation in c++
#include <iostream>
using namespace std;
template <class T>
class smart
{
protected :
T *ptr;
public :
explicit smart(T *data = NULL)
{
ptr = data;
}
~smart()
{
delete ptr;
}
T & operator *()
{
return *ptr;
}
};
class test {
public :
test()
{
cout << "test - constructor " << endl;
}
~test()
{
cout << "test - destroctor " << endl;
}
};
int main()
{
smart<int> obj(new int(20));
smart<char> obj2(new char('A'));
smart<test> obj3(new test());
cout << *obj << endl;
cout << endl;
cout << *obj2 << endl;
}
using namespace std;
template <class T>
class smart
{
protected :
T *ptr;
public :
explicit smart(T *data = NULL)
{
ptr = data;
}
~smart()
{
delete ptr;
}
T & operator *()
{
return *ptr;
}
};
class test {
public :
test()
{
cout << "test - constructor " << endl;
}
~test()
{
cout << "test - destroctor " << endl;
}
};
int main()
{
smart<int> obj(new int(20));
smart<char> obj2(new char('A'));
smart<test> obj3(new test());
cout << *obj << endl;
cout << endl;
cout << *obj2 << endl;
}
Comments
Post a Comment