智能指针
Posted Andromeda_Galaxy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了智能指针相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h>
using namespace std;
void swap(shared_ptr<int>a,shared_ptr<int>b)
{
int t;
t=*a,*a=*b,*b=t;
}
int main()
{
/*auto_ptr; unique_ptr; shared_ptr;
auto_ptr<int> p;
auto_ptr<int> p2(p);
auto_ptr<int> p3(new int(10));*/
auto_ptr<string> p1(new string("there is only one point to me."));
auto_ptr<string> p2;
cout<<*p1<<endl;
p2=p1;
//cout<<*p1<<endl;
cout<<*p2<<endl;
auto_ptr<string> p3(p2);
//cout<<*p2<<endl;
cout<<*p3<<endl;
unique_ptr<string> pl1(new string("auto"));
unique_ptr<string> pl2;
//unique_ptr<string> p3(p1);
//p2=p1
shared_ptr<int> p11(new int(9));shared_ptr<int> p22(p11);
shared_ptr<int> p33(new(int)),p44(new int(8)),p55;
cout<<*p11<<" "<<*p44<<endl; swap(p11,p44); cout<<*p11<<" "<<*p44<<endl;
//cout<<*p11<<" "<<*p22<<" "<<*p33<<" "<<*p44<<" "<<*p55<<endl;
}
以上是关于智能指针的主要内容,如果未能解决你的问题,请参考以下文章
C++编程经验:智能指针 -- 裸指针管得了的我要管,裸指针管不了的我更要管!