相关疑难解决方法(0)

如何声明std :: unique_ptr以及它的用途是什么?

我试着理解我是如何std::unique_ptr工作的,并且我找到了这个文件.作者从以下示例开始:

#include <utility>  //declarations of unique_ptr
using std::unique_ptr;
// default construction
unique_ptr<int> up; //creates an empty object
// initialize with an argument
unique_ptr<int> uptr (new int(3));
double *pd= new double;
unique_ptr<double> uptr2 (pd);
// overloaded * and ->
*uptr2 = 23.5;
unique_ptr<std::string> ups (new std::string("hello"));
int len=ups->size();
Run Code Online (Sandbox Code Playgroud)

令我困惑的是,在这一行

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

我们使用整数作为参数(在圆括号之间)和这里

unique_ptr<double> uptr2 (pd);
Run Code Online (Sandbox Code Playgroud)

我们使用指针作为参数.它有什么不同吗?

对我来说还不清楚的是,以这种方式声明的指针将如何与以"正常"方式声明的指针不同.

c++ pointers std unique-ptr

76
推荐指数
3
解决办法
11万
查看次数

标签 统计

c++ ×1

pointers ×1

std ×1

unique-ptr ×1