【发布时间】:2015-07-11 15:15:18
【问题描述】:
在声明 std::unique_ptr<std::string> 但没有分配它(因此它包含一个 std::nullptr 开头)之后 - 如何为它分配一个值(即我不再希望它持有 std::nullptr)?我尝试过的两种方法都不起作用。
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
其中another_str_var 是一个早先声明和分配的std::string。
显然,我对 std::unique_ptr 所做的事情的理解严重不足......
【问题讨论】:
标签: c++ string unique-ptr