Home » C++ » Copy constructor for a class with unique_ptr

Copy constructor for a class with unique_ptr

Posted by: admin November 30, 2017 Leave a comment

Questions:

How do I implement a copy constructor for a class that has a unique_ptr member variable? I am only considering C++11.

Answers:

Since the unique_ptr can not be shared, you need to either deep-copy its content or convert the unique_ptr to a shared_ptr.

class A
{
   std::unique_ptr< int > up_;

public:
   A( int i ) : up_( new int( i ) ) {}
   A( const A& a ) : up_( new int( *a.up_ ) ) {}
};

int main()
{
   A a( 42 );
   A b = a;
}

You can, as NPE mentioned, use a move-ctor instead of a copy-ctor but that would result in different semantics of your class. A move-ctor would need to make the member as moveable explicitly via std::move:

A( A&& a ) : up_( std::move( a.up_ ) ) {}

Having a complete set of the necessary operators also leads to

A& operator=( const A& a )
{
   up_.reset( new int( *a.up_ ) );
   return *this,
}

A& operator=( A&& a )
{
   up_ = std::move( a.up_ );
   return *this,
}

If you want to use your class in a std::vector, you basically have to decide if the vector shall be the unique owner of an object, in which case it would be sufficient to make the class moveable, but not copyable. If you leave out the copy-ctor and copy-assignment, the compiler will guide your way on how to use a std::vector with move-only types.

Questions:
Answers:

Try this helper to create deep copies, and cope when the source unique_ptr is null.

    template< class T >
    std::unique_ptr<T> copy_unique(const std::unique_ptr<T>& source)
    {
        return source ? std::make_unique<T>(*source) : nullptr;
    }

Eg:

class My
{
    My( const My& rhs )
        : member( copy_unique(rhs.member) )
    {
    }

    // ... other methods

private:
    std::unique_ptr<SomeType> member;
};

Questions:
Answers:

Daniel Frey mention about copy solution,I would talk about how to move the unique_ptr

#include <memory>
class A
{
  public:
    A() : a_(new int(33)) {}

    A(A &&data) : a_(std::move(data.a_))
    {
    }

    A& operator=(A &&data)
    {
      a_ = std::move(data.a_);
      return *this;
    }

  private:
    std::unique_ptr<int> a_;
};

They are called move constructor and move assignment

you could use them like this

int main()
{
  A a;
  A b(std::move(a)); //this will call move constructor, transfer the resource of a to b

  A c;
  a = std::move(c); //this will call move assignment, transfer the resource of c to a

}

You need to wrap a and c by std::move because they have a name
std::move is telling the compiler to transform the value to
rvalue reference whatever the parameters are
In technical sense, std::move is analogy to something like “std::rvalue”

After moving, the resource of the unique_ptr is transfer to another unique_ptr

There are many topics that document rvalue reference; this is a pretty easy one to begin with.

Edit :

The moved object shall remain valid but unspecified state.

C++ primer 5, ch13 also give a very good explanation about how to “move” the object

Questions:
Answers:

The usual case for one to have a unique_ptr in a class is to be able to use inheritance (otherwise a plain object would do as well, see RAII). For this case, there is no appropriate answer in this thread up to now.

So, here is the starting point:

struct Base
{
    //some stuff
};

struct Derived : public Base
{
    //some stuff
};

struct Foo
{
    std::unique_ptr<Base> ptr;  //points to Derived or some other derived class
};

… and the goal is, as said, to make Foo copiable.

For this, one needs to do a deep copy of the contained pointer to ensure the derived class is copied correctly.

This can be accomplished by adding the following code:

struct Base
{
    //some stuff

    auto clone() const { return std::unique_ptr<Base>(clone_impl()); }
protected:
    virtual Base* clone_impl() const = 0;
};

struct Derived : public Base
{
    //some stuff

protected:
    virtual Derived* clone_impl() const override { return new Derived(*this); };                                                 
};

struct Foo
{
    std::unique_ptr<Base> ptr;  //points to Derived or some other derived class

    //rule of five, but a user-defined dtor is not necessary due to unique_ptr
    Foo(Foo const& other) : ptr(other.ptr->clone()) {}
    Foo(Foo && other) = default;
    Foo& operator=(Foo const& other) { ptr = other.ptr->clone(); return *this; }
    Foo& operator=(Foo && other) = default;
};

There are basically two things going on here:

  • The first is the addition of copy and move constructors, which are implicitly deleted in Foo as the copy constructor of unique_ptr is deleted. The move constructor can be added simply by = default … which is just to let the compiler know that the usual move constructor shall not be deleted (this works, as unique_ptr already has a move constructor which can be used in this case).

    For the copy constructor of Foo, there is no similar mechanism as there is no copy constructor of unique_ptr. So, one has to construct a new unique_ptr, fill it with a copy of the original pointee, and use it as member of the copied class.

  • In case inheritance is involved, the copy of the original pointee must be done carefully. The reason is that doing a simple copy via std::unique_ptr<Base>(*ptr) in the code above would result in slicing, i.e., only the base component of the object gets copied, while the derived part is missing.

    To avoid this, the copy has to be done via the clone-pattern. The idea is to do the copy through a virtual function clone_impl() which returns a Base* in the base class. In the derived class, however, it is extended via covariance to return a Derived*, and this pointer points to a newly created copy of the derived class. The base class can then access this new object via the base class pointer Base*, wrap it into a unique_ptr, and return it via the actual clone() function which is called from the outside.

Leave a Reply

Your email address will not be published. Required fields are marked *