lundi 18 mai 2020

Smart Pointers in templates c++

Create a class template SmartPointer that should contain a pointer to any object and delete that same object when the destructor of that class is called. In order for the smart pointer to behave the same way the raw pointer behaves , you must overlap the operator * and ->. So, this is my task and i've done this code, but the -> operator is not okay, if someone knows how to fix it help me

template <class T>
class SmartPointer {
private:
    T* x;
public:
    T& operator*() {
        return *x;
    }
    T& operator->(){
        return this();
    }
    SmartPointer(T *X);
   ~SmartPointer();

};

template<class T>
SmartPointer<T>::SmartPointer(T *X) {
this->x = X;
}

template<class T>
SmartPointer<T>::~SmartPointer() {
delete x;
}

Aucun commentaire:

Enregistrer un commentaire