I have a std::vector<std::unique_ptr<BaseType>>
. Is there any clean way to do a deep copy of the vector
?
The only thing that I can think of is to have a function that uses dynamic_cast
to retrieve the derived type, and then copy that into a new object held by a unique_ptr
. Given that I have control of all possible derived classes, this would be feasible. This has all kinds of obvious shortcomings.
Previously I had used a single class that was a union of all derived types. In attempting to get away from this I have encountered the situation of needing to copy the vector
.
Is there any good solution to this problem? The only solutions that I can come up with are hideously ugly and cause me great shame to even consider. This is one largish step in attempting to refactor/cleanup code that I work with.
The vector
is a member of a class that must be copyable. So, in this case, I just need to ensure that I can write a copy constructor for the containing class.