0

I have following C++ code snippet, which implements a unique pointer logic:

template<typename T>
class unique_ptr {
private:
    T* _ptr;
public:
    unique_ptr(T& t) {
       _ptr = &t;
    }
    unique_ptr(unique_ptr<T>&& uptr) {
       _ptr = std::move(uptr._ptr);
       uptr._ptr = nullptr;
    }
    ~unique_ptr() {
       delete _ptr;
    }
    unique_ptr<T>& operator=(unique_ptr<T>&& uptr) {
       if (this == uptr) return *this;
       _ptr = std::move(uptr._ptr);
       uptr._ptr = nullptr;
       return *this;
    }

    unique_ptr(const unique_ptr<T>& uptr) = delete;
    unique_ptr<T>& operator=(const unique_ptr<T>& uptr) = delete;
};

I am not clear about the meaning/logic of last two statements :

unique_ptr(const unique_ptr<T>& uptr) = delete;
unique_ptr<T>& operator=(const unique_ptr<T>& uptr) = delete;

What are these statement doing? What is the significance of delete keyword here? Can someone explain please in detail.

regards, -sunil puranik

Sunil Puranik
  • 87
  • 1
  • 4
  • 5
    They delete the copy constructor and copy assignment operator. If you can copy the pointer, it's no longer unique. ¯\\_(ツ)_/¯ – JHBonarius Feb 02 '22 at 06:55

0 Answers0