# 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;
}

Comments

Popular posts from this blog

Dynamic Memory allocation in C++ for 1d, 2d and 3d array

Write Your Own String Class:

Printing the odd and even no using Pthread