Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何声明std::unique_ptr及其用途?_C++_Pointers_Std_Unique Ptr - Fatal编程技术网

C++ 如何声明std::unique_ptr及其用途?

C++ 如何声明std::unique_ptr及其用途?,c++,pointers,std,unique-ptr,C++,Pointers,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&

我试图了解
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();
#包括//unique\u ptr的声明
使用std::unique\u ptr;
//默认构造
独特的ptr up//创建一个空对象
//用参数初始化
独特的ptr uptr(新的int(3));
double*pd=新的double;
唯一的ptr uptr2(pd);
//重载*和->
*uptr2=23.5;
独特的ptr ups(新标准::字符串(“hello”);
int len=ups->size();
让我困惑的是,在这一行中

unique_ptr<int> uptr (new int(3));
unique_ptr uptr(新int(3));
我们使用整数作为参数(在圆括号之间),这里

unique_ptr uptr2(pd);
我们使用指针作为参数。这有什么区别吗


我还不清楚的是,以这种方式声明的指针与以“正常”方式声明的指针有何不同。

唯一指针在超出范围时保证会破坏它们管理的对象。

在这种情况下:

unique_ptr<double> uptr2 (pd);
unique_ptr uptr2(pd);
uptr2
超出范围时,
pd
将被销毁。这有助于通过自动删除来管理内存


unique_ptr uptr(新int(3))的情况
没有区别,除了原始指针没有分配给此处的任何变量。

unique的构造函数接受指向类型为
T
的对象的原始指针(因此,它接受
T*

在第一个示例中:

unique_ptr<int> uptr (new int(3));
指针存储在
pd
变量中

从概念上讲,没有任何变化(您正在从原始指针构造一个
唯一的\u ptr
),但第二种方法可能更危险,因为它允许您执行以下操作:

unique_ptr<double> uptr2 (pd);
// ...
unique_ptr<double> uptr3 (pd);
现在,
unique\u ptr
是一个模拟唯一所有权的智能指针,这意味着在程序中的任何时候都只有一个(拥有)指针指向指向指向的对象-这就是为什么
unique\u ptr
是不可复制的


只要您使用智能指针的方式不违反它们要求您遵守的隐式契约,您就可以保证不会泄漏内存,并且将强制执行对象的正确所有权策略。原始指针不能保证这一点。

在分配给唯一指针的两个概念中没有区别

int* intPtr = new int(3);
unique_ptr<int> uptr (intPtr);
在这里,您必须删除heapInt,然后使用。如果未将其删除,则会发生内存泄漏

为了避免此类内存泄漏,使用了unique_ptr,其中unique_ptr在heapInt超出范围时自动删除其占用的空间。因此,您不需要删除或释放unique_ptr。

从中,一个
std::unique_ptr
构造函数是

int* intPtr = new int(3);
unique_ptr<int> uptr (intPtr);
显式唯一_ptr(指针p)noexcept

因此,创建一个新的
std::unique_ptr
就是将一个指针传递给它的构造函数

unique_ptr<int> uptr (new int(3));

当您不再需要它时,它将导致内存泄漏。

new int(3)
返回指向新的
int
的指针,就像
pd
是指向新的
double
的指针一样。唯一的ptr声明位于
中,而不是
中,我无法理解有关
模型对象所有权
、代码中的
整数泄漏
强制对象所有权策略的任何内容。您能推荐一些主题/资源来学习这些概念吗?我不能使用
unique\u ptr
,否则会出现错误:
文本“>”是意外的。这可能是因为此标记旨在作为模板参数列表终止符,但其名称未知为模板。
,即使我有
\include
\include
。有什么建议吗?
{
    unique_ptr<int> p = make_unique<int>(42);
    // Going out of scope...
}
// I did not leak my integer here! The destructor of unique_ptr called delete
int* intPtr = new int(3);
unique_ptr<int> uptr (intPtr);
unique_ptr<int> uptr (new int(3));
int* heapInt = new int(5);//initialize int in heap memory
.
.//use heapInt
.
delete heapInt;
unique_ptr<int> uptr (new int(3));
int *int_ptr = new int(3);
std::unique_ptr<int> uptr (int_ptr);
delete int_ptr;