Arvind Sharma
Arvind Sharma

Reputation: 3

template for a smartpointer and auto deletion of the pointer

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

Upvotes: 0

Views: 105

Answers (2)

Aziuth
Aziuth

Reputation: 3902

"Break down line by line" is a little bit excessive, for the future, try to mention single parts that you don't understand in particular. But since it's not that much code...

#include <iostream>
using namespace std;
template <class T> //make it a template in order to be able to store any type
class SmartPtr {
T *ptr; //wrapped pointer
public: explicit SmartPtr(T *p = NULL) { ptr = p; } //basic constructor

//delete pointer when the object dies.
//Note that this means that this is meant to be a unique pointer, 
//a shared pointer would have to count occurencies first
 ~SmartPtr() { delete(ptr); } 

//dereference operator to manipulate the content. 
//I prefer to go with T& instead of T & to make it clear that it is
//"reference of type T"
T & operator * () {  return *ptr; }
//bad operator which exposes the pointer itself.
//Make this const, like const T* operator -> () const { ... }
//else this could be used for the constructor again,
//leading to undefined behaviour when the pointer is deleted twice
T * operator -> () { return ptr; } }; //class ending brace at bad position

int main(){
SmartPtr<int> ptr(new int());
*ptr = 20;
cout << *ptr;
return 0; }

Upvotes: 1

Akira
Akira

Reputation: 4473

Your class SmartPtr encapsulates a pointer of type T and overloads the member access and dereference operators to allow access to the encapsulated pointer. Moreover it frees up the the allocated memory pointed by encapsulated pointer when its descructor is called.

Your class with commens:

template <class T>
class SmartPtr {
    T* ptr; // encapsulated pointer of type T

public:
    // constructor for SmartPtr class which assigns the specified pointer to the
    // encapsulated pointer
    explicit SmartPtr(T* p = NULL) {
        ptr = p;
    }
    // destructor for SmartPtr class which frees up the the allocated memory
    // pointed by the encapsulated pointer
    ~SmartPtr() {
        delete(ptr);
    }
    // overloads the dereference operator for SmartPtr class to allow syntax
    // like: *instance = value;
    T& operator*() {
        return *ptr;
    }
    // overloads the member access operator for SmartPtr class to allow syntax
    // like: instance->member();
    T* operator->() {
        return ptr;
    }
};

Usage of your SmartPtr class is show inside the main function you provided:

SmartPtr<int> ptr(new int());
*ptr = 20;

The first line does a class template instantiation and constructs an object (ptr) by calling the contructor with newly created int as parameter.

The second line calls the overloaded dereference operator and assigns value 20 to the encapsulated pointer. This line is equivalent to:

ptr.operator*() = 20;

Upvotes: 1

Related Questions