mercredi 2 novembre 2016

C++ constructor dependency injection: unique_ptr + move vs shared_ptr

Let's assume we have something like this:

struct Complex{
    Complex(Part1 p1, Part2 p2, Part3 p3)
}

Part1 p1; 
Part2 p2;
Part3 p3; 

However passing copies not effective, so we need to move to pointers. The questions is what type to use - unique_ptr or shared_ptr

For the first view seems like Complex is a real owner of p1, p2, p3 and unique_ptr is better, however since it could not be copied, we need to use std::move

So my question is - what is a better way for that case, create unqiue_ptr out of Complex, and then use move in Complex's constructor, or create shared_ptr from the beggining and use shared_ptr instead?

Aucun commentaire:

Enregistrer un commentaire