定义
unique_ptr 是 C++ 11 提供的用于防止内存泄漏的智能指针中的一种实现,独享被管理对象指针所有权的智能指针。
int main()
{
std::unique_ptr<int> num(new int(23));
cout << *num << endl;
return 0;
}
move和get
std::move是将对象的状态或者所有权从一个对象转移到另一个对象,只是转移,没有内存的搬迁或者内存拷贝所以可以提高利用效率,改善性能.。
get函数会返回存储的指针。如果由unique_ptr不为空,则存储的指针指向由unique_ptr管理的对象,否则指向nullptr。
#include<string>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#include<algorithm>
#include<memory>
using namespace std;
int main()
{
unique_ptr<int> num(new int(23));
cout<<"value="<<*num<<" "<<" addr="<<num.get()<<endl;
unique_ptr<int> num1=move(num);
cout<<"value="<<*num1<<" "<<" addr="<<num1.get()<<endl;
return 0;
}
运行结果:
value=23 addr=0x5583e82e2e70
value=23 addr=0x5583e82e2e70
get函数
来源:CSDN
作者:tom-wei
链接:https://blog.csdn.net/chengbeng1745/article/details/104108319