Sobes.tech
Junior — Middle+

SharedPtr: implementation of copy constructor

livecode

Task condition

It is necessary to add a copy constructor to the smart pointer class that will correctly increase the reference count.

template <class T>
class SharedPtr {
    int* refCount = nullptr;
    T* rawPtr = nullptr;

public:
    SharedPtr() = default;

    SharedPtr(const SharedPtr& other) {
        rawPtr = other.rawPtr;
        refCount = other.refCount;
        // increase the reference count
    }

    SharedPtr(T* ptr) {
        rawPtr = ptr;
        refCount = new int(1);
    }
};