首页 > 其他 > 详细

智能指针

时间:2021-07-17 11:01:06      阅读:26      评论:0      收藏:0      [点我收藏+]
#include <bits/stdc++.h>
using namespace std;
//智能指针 
int main(){
    //unique_ptr,保证同一时间内只有一个智能指针可以指向该对象
    unique_ptr<string> p3(new string("auto"));
    unique_ptr<string> p4;
//    p4 = p3; 不允许,会产生相应的报错 
    unique_ptr<string> pu1(new string ("hello world"));
    unique_ptr<string> pu2;
//    pu2 = pu1;会产生报错,因为留下了悬挂的unique_ptr(pu1)
    unique_ptr<string> pu3;
    pu3 = unique_ptr<string> (new string ("You"));
// =号的左侧是个临时变量,让权给pu3后,便被自动销毁,编译器允许这样赋值

    unique_ptr<string> t1,t2;
    t1 = unique_ptr<string> (new string ("You"));
    t2 = move(t1);//借助move函数实现安全赋值 
    cout<<*t2<<endl;
    return 0; 
} 

 

智能指针

原文:https://www.cnblogs.com/zmachine/p/15021921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!