Search code examples
c++pointersunique-ptr

What is the behaviour of unique_ptr in this situatuion?


I am using a library that provides functions related to randomness (https://github.com/effolkronium/random#is-equal) and I applied one of them that will randomly choose between the 2 objects.

    Class object1;
    Class object2;
    std::unique_ptr<Class> p1 = std::make_unique<Class>(Random::get({object1, object2}));
    std::unique_ptr<Class> p2 = std::make_unique<Class>(Random::get({object1, object2}));

Because I am using unique_ptr is it certain that these two pointers will always point to a different object?


Solution

  • no matter what object is chosen (1 or 2) make_unique will call the copy constructor p1 will not point to object1 or object2 it will create a new object of type 'Class' and will point to that.

    -> p1 and p2 will always point to different objects (of type 'Class') after your lines, you have 4 different objects