If a member of a class needs polymorphic behavior it needs to be either a pointer or a reference. If this member is initialized with a constructor parameter and should live as long as the class I see two options.
Option 1: Copy reference parameter into smart pointer
class A
{
private:
std::unique_ptr<Type> _ptr;
public:
A(Type& parameter) : _ptr(std::make_unique<Type>(parameter)) {}
};
Option 2: use a smart pointer as paramter
class A
{
private:
std::unique_ptr<Type> _ptr;
public:
A(std::unique_ptr<Type> parameter) : _ptr(parameter) {}
};
I think option 1 has the advantage that the caller can pass any object whereas option 2 does not need to copy the object. My question is now which option of the two is preferable?