小编Ger*_*dos的帖子

这个unique_ptr的初始化有什么问题?

有人可以告诉我,unique_ptr的以下初始化有什么问题?

int main()
{
  unique_ptr<int> py(nullptr);
  py = new int;
  ....
}
Run Code Online (Sandbox Code Playgroud)

g ++ -O2 xxx.cc -lm -o xxx -std = c ++ 11说:

error: no match for ‘operator=’ (operand types are    ‘std::unique_ptr<int>’ and ‘int*’)
   py = new int;
      ^
Run Code Online (Sandbox Code Playgroud)

unique_ptr<int> px(new int);
Run Code Online (Sandbox Code Playgroud)

工作得很好.

c++ initialization unique-ptr c++11

6
推荐指数
2
解决办法
2220
查看次数

标签 统计

c++ ×1

c++11 ×1

initialization ×1

unique-ptr ×1