Could you implement a deep copy / copy assignment for the example above or link me a example ... I'm very new to this and don't want to mess things up by doing un/miss informed stuff.
> I've got a class with a member of type std::vector<std::unique_ptr<MyClass>>
> and I need it to be fully copy-able.
Then use a copy_ptr instead
> Is the class in question (A) a polymorphic type
>> No its not. No virtual functions at all.
¿why do you want pointers then?
> EDIT: I think its important to say that the ownership of all pointers is supposed to get transfered
Oh, so you do not want a copy, but a transfer
Use transfer_owner_ptr then.
use a copy_ptr instead
[snip]
Use transfer_owner_ptr
Are you making these up? Or are those part of boost or something?
I've never heard of them before, and google search comes up empty.
Anyway, @ OP... you have 2 choices that I can see. Each do slightly different things, so pick whichever one is closer to what you need:
Option 1) Use shared_ptr instead of unique_ptr
shared_ptr can be copied, which will make your vector copyable (and therefore your class copyable). However each vector will "share" ownership of the pointed to object.
Example:
1 2 3 4 5 6 7 8 9 10 11 12
std::vector<std::shared_ptr<int>> v;
v.emplace_back( newint(1) );
std::vector<std::shared_ptr<int>> cpy = v; // OK
cout << *v[0]; // prints 1
cout << *cpy[0]; // also prints 1
*v[0] = 5;
cout << *v[0]; // prints 5 as you'd expect
cout << *cpy[0]; // ALSO prints 5 because it points to the same int
This is a shallow copy with shared ownership.
Option 2) Do a deep copy of your unique_ptr vector
This involves iterating over the entire vector and creating a copy of each object in the vector.
std::vector<std::unique_ptr<int>> v;
std::vector<std::unique_ptr<int>> cpy;
v.emplace_back( newint(1) );
// begin deep copy code:
cpy.reserve( v.size() ); // <- optional bit to improve performance
for(auto& i : v)
cpy.emplace_back( newint(*i) );
// now that each int has been copied, you have a deep copied vector:
cout << *v[0]; // prints 1
cout << *cpy[0]; // also prints 1
*v[0] = 5;
cout << *v[0]; // prints 5
cout << *cpy[0]; // still prints 1... this time 'cpy' has its own ints
Note that as others have mentioned, the object copying may not be as simple as line 9 above if the object you're copying is of a different type than the pointer. Having virtual functions doesn't really matter... what matters is whether or not the pointer actually IS the type (ie... you can't point to any derived class.. you must point only to the given class)
I'm making them up
The idea is that instead of writing a copy constructor, assignment operator and destructor you make the elements behave as you wish.
So you only need to code an appropriate wrapper for the members.
When you do a = b; where `a' and `b' are "smart" pointers there are several things that may happen
- both refer to the same object. According to who is responsible of the destruction you may have
-- the last one to live have to kill it ( shared_ptr )
-- there is one owner, if it dies the dangling references are set to null ( weak_ptr I think )
- `a' points to another object, that compares equal to the one that `b' points. A clone if you wish ( copy_ptr )
- `a' is responsible for the destruction, `b' now holds nothing ( transfer_owner_ptr, similar to std::auto_ptr or std::unique_ptr )