C++11 智能指针

时间:2020-07-23 22:31:29   收藏:0   阅读:74

前言

auto_prt 所有权模式

auto_ptr<string> ap1 (new string ("hello,world")); 
auto_ptr<string> ap2;
ap2=ap1
//这里不会报错,ap2剥夺ap1的权限,但程序访问ap1会报错,ap1被废弃。auto_ptr缺点是:存在潜在的程序崩溃问题。

unique_ptr 独占指针


unique_ptr<int> up1(new int(2333));
unique_ptr<int> up2 = up1;//这里会报错,尝试引用已删除的函数。

//将原来up1指向内存交由up3指向
unique_ptr<int> up3 = std::move(up1);
cout << *up3 << endl;
cout << *up1 << endl;//内部已经将原来的指向设置为了nullptr但是并没有删除

//如果是reset带参调用
unique_ptr<int> up4(new int(2333));
up4.reset(new int(9999));//将原来指向的2333删除掉重新指向9999
cout << *up4 << endl;

up4 = nullptr;//相当于up4.reset(nullptr);
//cout << *up4 << endl;

unique_ptr<int> up5(new int(777));
int* p5 = up5.release();//将内存的控制权交给接受的指针,自己赋值为nullptr,但是没有删除
//cout << *up5 << endl;//已经失去了对之前内存的控制
cout << *p5 << endl;
delete p5;//需要手动释放

shared_ptr 共享指针

shared_ptr<int> sp1(new int(123));
cout << sp1.use_count() << endl;
shared_ptr<int> sp2 = sp1;//增加了引用计数
cout << sp2.use_count() << endl;
cout << *sp1 << " " << *sp2 << endl;

sp1.reset();//对计数进行了减1,并且sp1退出了对共享的内存的管理
cout << sp2.use_count() << endl;//1
cout << sp1.use_count() << endl;//0
cout << sp2.unique() << endl;//判断当前是否为独占(是否只有一个引用对象)

sp1 = sp2;
cout << sp2.use_count() << endl;//2
cout << sp1.use_count() << endl;//2
//共享指针之间可以交换
shared_ptr<int> sp3(new int(456));
sp2.swap(sp3);//将sp2指向和sp3指向进行了交换,对应的引用计数的变化要清楚
cout << sp1.use_count() << endl;//2
cout << sp2.use_count() << endl;//1
cout << sp3.use_count() << endl;//2
shared_ptr<int> sp4 = sp2;
//获取共享指针的原始指针
int* p = sp2.get();
cout << p[0] << endl;
p[0] = 321;
cout << *sp4 << endl;

//安全的使用共享指针,推荐使用make_shared函数来进行共享指针对象的创建
shared_ptr<int> sp5 = make_shared<int>();
cout << *sp5 << endl;
shared_ptr<int> sp6 = make_shared<int>(9527);
cout << *sp6 << endl;
shared_ptr<string> sp7 = make_shared<string>(10, ‘9‘);
cout << *sp7 << endl;

weak_ptr 弱指针

shared_ptr<A> aa = make_shared<A>();
shared_ptr<B> bb = make_shared<B>();
//执行结束以后内存没有被释放,循环引用导致的
aa->b = bb;//2
bb->a = aa;//2

//改用弱指针可以消除循环引用的弊端
aa->wb = bb;//1
bb->wa = aa;//1

总结

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!