Copy constructor of a class containing a smart pointer

271 views Asked by At

In the following example we have a class Class that contains a Bridge object that takes care of all the memory handling for us (rule of three).

class Base {
public:
    Base() {};
    virtual Base* clone() const = 0;
    virtual ~Base() {};
};

class Derived : public Base {
public:
    Derived() {};
    virtual Base* clone() const {
        return new Derived(*this);
    }
    virtual ~Derived() {}
};

class Bridge {
public:
    Bridge(const Bridge& bridge_) {
        base = bridge_.base->clone();
    }
    Bridge(const Base& base_) {
        base = base_.clone();
    }
    ~Bridge() { delete base; }
    
    Bridge& operator=(const Bridge& assignFrom) {
        if(this != &assignFrom) {
            delete base;
            base = assignFrom.base->clone();
        }
        return *this;
    }
private:
    Base *base;
};

class Class {
public:
    Class(const Bridge& bridge_) : bridge(bridge_) {};
private:
    Bridge bridge;
};

int main()
{
    Derived derived;
    Class c(derived);
    Class c1(c);  
}

Now, I have just learned about smart pointers and was trying to recreate the above example using unique_ptr. To my understanding, we basically don't need to implement the rule of 3 ourselves as the smart pointer contains it already. To test this, I made the following example:

class BaseSMRT {
public:
    BaseSMRT() {};
    virtual std::unique_ptr<BaseSMRT> clone() const = 0;
    virtual ~BaseSMRT() {};
};

class DerivedSMRT : public BaseSMRT {
public:
    DerivedSMRT() {};
    virtual std::unique_ptr<BaseSMRT> clone() const {
        return std::make_unique<DerivedSMRT>(*this);
    }
    virtual ~DerivedSMRT() {}
};

class ClassSMRT {
public:
    ClassSMRT(const BaseSMRT& base) {
        ptr = base.clone();
    };
private:
    std::unique_ptr<BaseSMRT> ptr;
};

int main()
{
    DerivedSMRT derivedSMRT;
    ClassSMRT cSMRT(derivedSMRT);
    ClassSMRT cSMRT2(cSMRT); // error: Call to implicitly-deleted copy constructor of 'ClassSMRT'
}

As you can see in the above example, initialising cSMRT2 with cSMRT through the copy constructor doesn't work and gives me the above error.

I don't get this: Why is it that I can call Class's default copy constructor like this, Class c1(c);, but not call ClassSMRT's default copy constructor, ClassSMRT cSMRT2(cSMRT);?

This suggests that the rule of three isn't already implemented for us when we are using unique_ptr.

1

There are 1 answers

3
Caleth On

unique_ptr is designed to stop accidental copies. You want to implicitly clone you polymorphic type when copied. unique_ptr doesn't (directly) fit your uses.

I would suggest a rename of Bridge

template </*Cloneable*/ typename T>
class clone_ptr {
public:
    clone_ptr(const T& base_) 
      : base(base_.clone()) {}
    clone_ptr(const clone_ptr& other)
      : base(other.base->clone()) {}
    clone_ptr(clone_ptr&& other) = default;
    
    clone_ptr& operator=(clone_ptr other) {
        base = std::move(other.base);
        return *this;
    }
private:
    std::unique_ptr<T> base;
};