1.auto_ptr
采用所有权模式,已被C++11废弃
2.unique_ptr
作为替代auto_ptr而出现的智能指针,采用比auto_ptr更严格的独占模式(同一时间段内只允许一个智能指针指向对象)。
auto_ptr<string> p1(new string("hello world"));
auto_ptr<string> p2;
p2=p1;//不会报错
unique_ptr<string> p3(new string("hello world"));
unique_ptr<string> p4;
p4=p3;//会报错
unique_ptr<string> p5;
//匿名临时对象生命周期只有一行
p5=unique_ptr<string>(new string("hello world"));
3.share_ptr
顾名思义,share_ptr作为能够实现多个智能指针之间数据共享而出现,允许多个指针指向同一对象 -> 采用引用计数的方式,当计数为0时释放对象资源。
4.weak_ptr
为了解决share_ptr相互引用而导致的死锁问题(引用计数不可能为0,资源无法得到释放)而出现的智能指针,跟share_ptr搭配使用。
658

被折叠的 条评论
为什么被折叠?



