auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
2017-06-05 11:23
627 查看
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。CSDN视频网址:http://edu.csdn.net/lecturer/144 C ++库提供以下类型的智能指针的实现: auto_ptr
unique_ptr
shared_ptr
weak_ptr
它们都是在内存头文件中声明。
auto_ptr是一个智能指针,用于管理通过新表达式获取的对象,并在auto_ptr本身被销毁时删除该对象。
当使用auto_ptr类描述一个对象时,它存储一个指向单个分配对象的指针,该对象可以确保当它超出范围时,它指向的对象必须被自动销毁。 它基于独占所有权模式,即同一类型的两个指针不能同时指向相同的资源。 如下面的程序所示,复制或分配指针会更改所有权,即源指针必须赋予目标指针所有权。
#include<iostream>
#include<memory>
using namespace std;
class A
{
public:
void show() { cout << "A::show()" << endl; }
};
int main()
{
// p1 is an auto_ptr of type A
auto_ptr<A> p1(new A);
p1 -> show();
// returns the memory address of p1
cout << p1.get() << endl;
// copy constructor called, this makes p1 empty.
auto_ptr <A> p2(p1);
p2 -> show();
// p1 is empty now
cout << p1.get() << endl;
// p1 gets copied in p2
cout<< p2.get() << endl;
return 0;
}Output:A::show()
0x1b42c20
A::show()
0
0x1b42c20auto_ptr的拷贝构造函数和赋值运算符实际上并没有复制存储的指针,而是将它们传输出去,使第一个auto_ptr对象变为空。 这是实现严格所有权的一种方法,因此只有一个auto_ptr对象可以在任何给定时间拥有该指针,即在需要复制语义的情况下不应使用auto_ptr。
所以,当使用unique_ptr时,在任何一个资源上最多只能有一个unique_ptr,当该unique_ptr被破坏时,该资源将被自动声明。 另外,由于任何资源只能有一个unique_ptr,所以任何创建unique_ptr副本的尝试将导致编译时错误。 unique_ptr<A> ptr1 (new A);
// Error: can't copy unique_ptr
unique_ptr<A> ptr2 = ptr1; 但是,unique_ptr可以使用新的语义,即使用std :: move()函数将包含的指针的所有权转移到另一个unique_ptr。
// Works, resource now stored in ptr2
unique_ptr<A> ptr2 = move(ptr1); 所以,最好使用unique_ptr,当我们想要一个指向一个对象的指针,当该单个指针被销毁时将被回收。
0x1c4ac20
A::show()
0 // NULL
0x1c4ac20
A::show()
0 // NULL
0 // NULL
0x1c4ac20下面的代码返回一个资源,如果我们没有显式捕获返回值,资源将被清除。 如果我们这样做,那么我们拥有该资源的独占所有权, 这样我们可以将unique_ptr看作更安全,更好的替换auto_ptr。
unique_ptr<A> fun()
{
unique_ptr<A> ptr(new A);
/* ...
... */
return ptr;
}何时使用unique_ptr?
当您想拥有资源的唯一所有权(Exclusive)时,请使用unique_ptr, 只有一个unique_ptr可以指向一个资源, 因为单个资源可以有一个unique_ptr,所以不可能将一个unique_ptr复制到另一个。
引用计数:这是一种将资源数量,指针或句柄存入资源(如对象,内存块,磁盘空间或其他资源)的技术。
引用计数大于0,直到所有的shared_ptr副本都被删除,所包含的原始指针引用的对象将不会被销毁。因此,当我们要为一个原始指针分配给多个所有者时,我们应该使用shared_ptr。 何时使用shared_ptr?
本篇博客主要是给读者对智能指针的使用做一个总结。。。。。。。。
unique_ptr
shared_ptr
weak_ptr
它们都是在内存头文件中声明。
auto_ptr
从C ++ 11开始,此类模板已被弃用。 unique_ptr是具有相似功能但具有改进的安全性的新工具。auto_ptr是一个智能指针,用于管理通过新表达式获取的对象,并在auto_ptr本身被销毁时删除该对象。
当使用auto_ptr类描述一个对象时,它存储一个指向单个分配对象的指针,该对象可以确保当它超出范围时,它指向的对象必须被自动销毁。 它基于独占所有权模式,即同一类型的两个指针不能同时指向相同的资源。 如下面的程序所示,复制或分配指针会更改所有权,即源指针必须赋予目标指针所有权。
#include<iostream>
#include<memory>
using namespace std;
class A
{
public:
void show() { cout << "A::show()" << endl; }
};
int main()
{
// p1 is an auto_ptr of type A
auto_ptr<A> p1(new A);
p1 -> show();
// returns the memory address of p1
cout << p1.get() << endl;
// copy constructor called, this makes p1 empty.
auto_ptr <A> p2(p1);
p2 -> show();
// p1 is empty now
cout << p1.get() << endl;
// p1 gets copied in p2
cout<< p2.get() << endl;
return 0;
}Output:A::show()
0x1b42c20
A::show()
0
0x1b42c20auto_ptr的拷贝构造函数和赋值运算符实际上并没有复制存储的指针,而是将它们传输出去,使第一个auto_ptr对象变为空。 这是实现严格所有权的一种方法,因此只有一个auto_ptr对象可以在任何给定时间拥有该指针,即在需要复制语义的情况下不应使用auto_ptr。
unique_ptr
std :: unique_ptr是在C ++ 11中开发的,用于替代std :: auto_ptr。unique_ptr是具有类似功能的新工具,但具有改进的安全性(无假拷贝分配),添加功能(删除器)和数组支持。 它是一个原始指针的容器。 它明确地防止复制其包含的指针,正如正常赋值那样会发生,即它只允许底层指针的一个所有者。所以,当使用unique_ptr时,在任何一个资源上最多只能有一个unique_ptr,当该unique_ptr被破坏时,该资源将被自动声明。 另外,由于任何资源只能有一个unique_ptr,所以任何创建unique_ptr副本的尝试将导致编译时错误。 unique_ptr<A> ptr1 (new A);
// Error: can't copy unique_ptr
unique_ptr<A> ptr2 = ptr1; 但是,unique_ptr可以使用新的语义,即使用std :: move()函数将包含的指针的所有权转移到另一个unique_ptr。
// Works, resource now stored in ptr2
unique_ptr<A> ptr2 = move(ptr1); 所以,最好使用unique_ptr,当我们想要一个指向一个对象的指针,当该单个指针被销毁时将被回收。
// C++ program to illustrate the use of unique_ptr #include<iostream> #include<memory> using namespace std; class A { public: void show() { cout<<"A::show()"<<endl; } }; int main() { unique_ptr<A> p1 (new A); p1 -> show(); // returns the memory address of p1 cout << p1.get() << endl; // transfers ownership to p2 unique_ptr<A> p2 = move(p1); p2 -> show(); cout << p1.get() << endl; cout << p2.get() << endl; // transfers ownership to p3 unique_ptr<A> p3 = move (p2); p3->show(); cout << p1.get() << endl; cout << p2.get() << endl; cout << p3.get() << endl; return 0; }Output:A::show()
0x1c4ac20
A::show()
0 // NULL
0x1c4ac20
A::show()
0 // NULL
0 // NULL
0x1c4ac20下面的代码返回一个资源,如果我们没有显式捕获返回值,资源将被清除。 如果我们这样做,那么我们拥有该资源的独占所有权, 这样我们可以将unique_ptr看作更安全,更好的替换auto_ptr。
unique_ptr<A> fun()
{
unique_ptr<A> ptr(new A);
/* ...
... */
return ptr;
}何时使用unique_ptr?
当您想拥有资源的唯一所有权(Exclusive)时,请使用unique_ptr, 只有一个unique_ptr可以指向一个资源, 因为单个资源可以有一个unique_ptr,所以不可能将一个unique_ptr复制到另一个。
shared_ptr
shared_ptr是原始指针的容器。 它是一个引用计数模型,即它与shared_ptr的所有副本合作维护其包含的指针的引用计数。 因此,每当一个新的指针指向资源时,计数器就会递增,当调用对象的析构函数时递减计数器。引用计数:这是一种将资源数量,指针或句柄存入资源(如对象,内存块,磁盘空间或其他资源)的技术。
引用计数大于0,直到所有的shared_ptr副本都被删除,所包含的原始指针引用的对象将不会被销毁。因此,当我们要为一个原始指针分配给多个所有者时,我们应该使用shared_ptr。 何时使用shared_ptr?
weak_ptr
什么时候使用weak_ptr?本篇博客主要是给读者对智能指针的使用做一个总结。。。。。。。。
相关文章推荐
- C++中的智能指针——auto_ptr, unique_ptr, shared_ptr和weak_ptr
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
- 浅谈智能指针auto_ptr/shared_ptr/unique_ptr
- C++14 智能指针unique_ptr、shared_ptr、weak_ptr
- C++ 智能指针 shared_ptr unique_ptr weak_ptr
- C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)
- C++学习之智能指针--auto_ptr、scoped_ptr、scoped_array、shared_ptr、shared_array、weak_ptr
- c++11 智能指针 unique_ptr、shared_ptr与weak_ptr
- 智能指针 auto_ptr、scoped_ptr、shared_ptr、weak_ptr
- 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结
- C++智能指针:auto_ptr、shared_ptr、weak_ptr等
- C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)
- C++ 智能指针shared-ptr,unique_ptr和weak-ptr
- C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)
- 智能指针:unique_ptr、shared_ptr、weak_ptr
- 几种智能指针的比较(std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::weak_ptr)
- 详解C++各种智能指针: auto_ptr, shared_ptr, weak_ptr, scoped_ptr
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- 智能指针 std::auto_ptr 和 shared_ptr