I have come across this simple code in a website. I didn't understand it fully. It would be very helpful for me if someone can breakdown the code line by line and explain.
This is a code for creating a template of a smart pointer (auto deletion of pointer is the main motto to create such a pointer)
#include <iostream>
using namespace std;
template <class T>
class SmartPtr {
T* ptr;
public:
explicit SmartPtr(T* p = NULL) {
ptr = p;
}
~SmartPtr() {
delete(ptr);
}
T& operator*() {
return *ptr;
}
T* operator->() {
return ptr;
}
};
int main() {
SmartPtr<int> ptr(new int());
*ptr = 20;
cout << *ptr;
return 0;
}