After declaring an std::unique_ptr<std::string>
but without assigning it (so it contains an std::nullptr
to begin with) - how to assign a value to it (i.e. I no longer want it to be holding std::nullptr
)? Neither of the methods I've attempted work.
std::unique_ptr<std::string> my_str_ptr;
my_str_ptr = new std::string(another_str_var); // compiler error
*my_str_ptr = another_str_var; // runtime error
where another_str_var
is an std::string
that is declared and assigned earlier.
Clearly my understanding of what std::unique_ptr
is doing is woefully inadequate...