简体   繁体   中英

Unique copy of vector<unique_ptr>

I have a class object which contains a vector<unique_ptr> . I want a copy of this object to run non-const functions on. The original copy must remain const .

What would the copy constructor for such a class look like?

class Foo{
public:
 Foo(const Foo& other): ??? {}

 std::vector<std::unique_ptr> ptrs;
};

You cannot simply copy a std::vector<std::unique_ptr> because std::unique_ptr is not copyable so it will delete the vector copy constructor.

If you do not change the type stored in the vector then you could make a "copy" by creating a whole new vector like

std::vector<std::unique_ptr<some_type>> from; // this has the data to copy
std::vector<std::unique_ptr<some_type>> to;
to.reserve(from.size()) // preallocate the space we need so push_back doesn't have to

for (const auto& e : from)
    to.push_back(std::make_unique<some_type>(*e));

Now to is a separate copy of from and can be changed independently.


Additionally: If your type is polymorphic the above won't work as you would have a pointer to the base class. What you would have to do is make a virtual clone member function and have clone return a std::unique_ptr to a copy of the actual derived object. That would make the code look like:

std::vector<std::unique_ptr<some_type>> from; // this has the data to copy
std::vector<std::unique_ptr<some_type>> to;
to.reserve(from.size()) // preallocate the space we need so push_back doesn't have to

for (const auto& e : from)
    to.push_back(e->clone());

Nathan's answer above is great. Additionally in the the polymorphic case, I found this site useful for defining the clone() member function.

I used

std::unique_ptr<Base> Base::clone()
{
  return std::make_unique<Derived>(*this);
}

which is similar to Nathan's later comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM