此头文件是动态内存管理库的一部分。std::shared_ptr
是一种通过指针保持对象共享所有权的智能指针。多个 shared_ptr
对象可持有同一对象。下列情况之一出现时销毁对象并解分配其内存:
- 最后剩下的持有对象的
shared_ptr
被销毁; - 最后剩下的持有对象的
shared_ptr
被通过 operator= 或 reset() 赋值为另一指针。
成员函数
(构造函数) | 构造新的 shared_ptr (公开成员函数) |
(析构函数) | 如果没有更多 shared_ptr 指向所持有的对象,则析构该对象(公开成员函数) |
operator= | 对 shared_ptr 赋值(公开成员函数) |
修改器 | |
reset | 替换所管理的对象 (公开成员函数) |
swap | 交换所管理的对象 (公开成员函数) |
观察器 | |
get | 返回存储的指针 (公开成员函数) |
operator*operator-> | 解引用存储的指针 (公开成员函数) |
operator[] (C++17) | 提供到所存储数组的索引访问 (公开成员函数) |
use_count | 返回 shared_ptr 所指对象的引用计数(公开成员函数) |
unique (C++20 前) | 检查所管理对象是否仅由当前 shared_ptr 的对象管理(公开成员函数) |
operator bool | 检查是否有关联的管理对象 (公开成员函数) |
owner_before | 提供基于持有者的共享指针排序 (公开成员函数) |
示例代码
#include <iostream>
#include <memory>struct C2 { int* data; };
struct C3 { int a; int b; };int main()
{// shared_ptr constructor examplestd::shared_ptr<int> p1;std::shared_ptr<int> p2(nullptr);std::shared_ptr<int> p3(new int);std::shared_ptr<int> p4(new int, std::default_delete<int>());std::shared_ptr<int> p5(new int, [](int* p) {delete p; }, std::allocator<int>());std::shared_ptr<int> p6(p5);std::shared_ptr<int> p7(std::move(p6));std::shared_ptr<int> p8(std::unique_ptr<int>(new int));std::shared_ptr<C2> obj(new C2);std::shared_ptr<int> p9(obj, obj->data);std::cout << "use_count:\n";std::cout << "p1: " << p1.use_count() << '\t';std::cout << "p2: " << p2.use_count() << '\t';std::cout << "p3: " << p3.use_count() << '\t';std::cout << "p4: " << p4.use_count() << '\t';std::cout << "p5: " << p5.use_count() << '\n';std::cout << "p6: " << p6.use_count() << '\t';std::cout << "p7: " << p7.use_count() << '\t';std::cout << "p8: " << p8.use_count() << '\t';std::cout << "p9: " << p9.use_count() << '\n';// shared_ptr destructor exampleauto deleter = [](int *p) {std::cout << "[delete called]\n"; delete p;};std::shared_ptr<int> foo(new int, deleter);std::cout << "use_count:" << foo.use_count() << '\n';// shared_ptr::operator= examplestd::shared_ptr<int> foo2;std::shared_ptr<int> bar2(new int(10));foo2 = bar2; //copybar2 = std::make_shared<int>(20); //movestd::unique_ptr<int> unique(new int(30));foo2 = std::move(unique); //move from unique_ptrstd::cout << "*foo2:" << *foo2 << '\t';std::cout << "*bar2:" << *bar2 << '\n';// shared_ptr::reset examplestd::shared_ptr<int> sp; //emptysp.reset(new int); //takes ownership of pointer*sp = 100;std::cout << *sp << '\n';sp.reset(new int); //deletes managed object, acquires new pointer*sp = 200;std::cout << *sp << '\n';// shared_ptr::swap examplestd::shared_ptr<int> foo3(new int(101));std::shared_ptr<int> bar3(new int(201));foo3.swap(bar3);std::cout << "*foo3:" << *foo3 << '\t';std::cout << "*bar3:" << *bar3 << '\n';// shared_ptr::get exampleint *p = new int(10);std::shared_ptr<int> a(p);if(a.get() == p)std::cout << "a and p point to the same location\n";// three ways of accessing the same address:std::cout << *a.get() << "\t";std::cout << *a << "\t";std::cout << *p << "\n";// shared_ptr::operator*std::shared_ptr<int> foo4(new int);std::shared_ptr<int> bar4(new int(100));*foo4 = *bar4 * 2;std::cout << "foo4: " << *foo4 << '\t';std::cout << "bar4: " << *bar4 << '\n';// shared_ptr::operator->std::shared_ptr<C3> foo5;std::shared_ptr<C3> bar5(new C3);foo5 = bar5;foo5->a = 10;bar5->b = 20;if (foo5) std::cout << "foo5: " << foo5->a << ' ' << foo5->b << '\t';if (bar5) std::cout << "bar5: " << bar5->a << ' ' << bar5->b << '\n';// shared_ptr::uniquestd::shared_ptr<int> foo6;std::shared_ptr<int> bar6(new int);std::cout << "foo6 unique?\n" << std::boolalpha;std::cout << "1: " << foo6.unique() << '\t'; // false (empty)foo6 = bar6;std::cout << "2: " << foo6.unique() << '\t'; // false (shared with bar)bar6 = nullptr;std::cout << "3: " << foo6.unique() << '\n'; // true// example of shared_ptr::operator boolstd::shared_ptr<int> foo7;std::shared_ptr<int> bar7(new int(34));if (foo7) std::cout << "foo7 points to " << *foo7 << '\n';else std::cout << "foo7 is null\n";if (bar7) std::cout << "bar7 points to " << *bar7 << '\n';else std::cout << "bar7 is null\n";// shared_ptr::owner_beforeint *p10 = new int(10);std::shared_ptr<int> a10(new int(20));std::shared_ptr<int> b10(a10, p10); // alias constructorstd::cout << "comparing a11 and b10...\n" << std::boolalpha;std::cout << "value-based: " << (!(a10 < b10) && !(b10 < a10)) << '\n';std::cout << "owner-based: " << (!a10.owner_before(b10) && !b10.owner_before(a10)) << '\n';delete p10;return 0;
}
运行效果:
参考:
https://cplusplus.com/reference/memory/shared_ptr/
std::shared_ptr - cppreference.com