1. 相关概念
1.1 使用场景
• 程序不知道自己需要使用多少对象
• 程序不知道所需对象的准确类型
• 程序需要在多个对象间共享数据
1.2 实现原理
- shared_ptr对象有一个关联的计数器,通常称为引用计数。
- 某些操作会递增计数器,
拷贝一个shared_ptr, 例如一个shared_ptr初始化另一个shared_ptr(作为函数参数, 作为函数返回值) - 某些操作计数器会递减
给shared_ptr赋一个新值, shared_ptr被销毁时 - 一旦一个shared_ptr的计数器变为0,它就会自动释放自己所管理的对象。
2. 主要操作
操作 | 说明 |
---|---|
make_shared(args) | 返回一个shared_ptr,指向一个动态分配的类型为T的对象。使用args初始化此对象 |
shared_ptr p(q) | p是shared_ptr q的拷贝;此操作会递增q中的计数器。q中的智能指针必须转化为*T |
p=q | p和q都是shared_ptr,所保存的指针必须能够相互转换。此操作会递减p的引用计数,递增q的引用计数;若p的引用计数变为0,则将其管理的原内存释放 |
p.unique() | 若p.use_count()为1,返回true;否则返回false |
p.use_count() | 返回与p共享对象的智能指针数量;可能很慢,主要用于调试 |
2.1 定义
eg.
shared_ptr<string> p1; //shared_ptr,可以指向string
shared_ptr<list<int>> p2; //shared_ptr,可以指向int的list
//使用make_shared函数
shared_ptr<int> p3 = make_shared<int>(42);
shared_ptr<string> p4 = make_shared<string>(10, '9');
shared_ptr<int> p5 = make_shared<int>();
auto p6 = make_shared<vector<string>>();
shared_ptr<string>sp;
make_shared<string>(); //动态分配内存默认初始化,必须要有括号, 默认初始化得到的是空指针
make_shared<string>("a"); //动态分配内存值初始化
shared_ptr<string>sp2 = make_shared<string>(); //初始化智能指针
shared_ptr<string>sp3 = make_shared<string>("b");//初始化智能指针
//和new结合
shared_ptr<int> p1=new int(1024); //error
shared_ptr<int> p2(new int(1024)); //ok, 直接初始化形式。
2.2 copy和赋值
auto r = make_shared<int>(42); // r指向的int只有一个引用者
r = q;
// r指向了q所指向的地址
// 递增q所指向对象的引用计数
// 递减r原来所指向对象的引用计数
// r原来所指向对象的引用计数为0,即没有引用者,自动释放
eg.
//传递参数会构造一个,计数器递增,函数运行结束后释放
shared_ptr<string> fun1(shared_ptr<string> sp5)
{
auto sp6 = sp5; //创建临时并赋值,计数器递增。
cout << "sp5 use_count:" << sp5.use_count() << endl;
cout << "sp5 is_unique:" << sp5.unique() << endl;
return sp6;
}
int main()
{
shared_ptr<string>sp = make_shared<string>("aa");
//通常使用auto来简化定义一个对象来保存make_shared的结果,这种方式比较简单。
auto sp3 = make_shared<string>(10,'a');
cout << "sp use_count:" << sp.use_count() << endl;
auto sp2(sp); //拷贝sp,count计数会增加
cout << "sp use_count:" << sp.use_count() << endl;
cout << "sp is_unique:" << sp.unique() << endl;
sp2 = sp3; //赋值sp2,计数会减少
cout << "sp use_count:" << sp.use_count() << endl;
cout << "sp is_unique:" << sp.unique() << endl;
auto sp4(sp3);
cout << "sp3 use_count:" << sp3.use_count() << endl;
cout << "sp3 is_unique:" << sp3.unique() << endl;
sp = sp3;//sp指向sp3指向的,sp指向的被销毁。
cout << "sp use_count:" << sp.use_count() << endl;
cout << "sp is_unique:" << sp.unique() << endl;
auto sp7 = fun1(sp);
cout << "sp7 use_count:" << sp.use_count() << endl;
cout << "sp7 is_unique:" << sp.unique() << endl;
}
输出结果:
3. 其它定义方法和reset方法
操作 | 说明 |
---|---|
shared_ptr p(q) | p管理内置指针q所指的对象;q必须指向new分配的内存,且能够转换为T*类型 |
shared_ptr p(u) | p从unique_ptr u中接管了对象的所有权;将u置为空 |
shared_ptr p(q,d) | p接管了内置指针q所指向的对象的所有权。q必须能转换为T*类型。p将使用可调用对象d来代替delete |
shared_ptr p(p2,d) | p是shared_ptr p2的拷贝,唯一的区别是p将使用可调用对象d来代替delete |
p.reset() | 若p是唯一指向其对象的shared_ptr,reset会释放此对象。 |
p.reset(q) | 释放原有对象的同时,若传递了可选的内置参数指针q,会令p指向q,否则会将p置为空 |
p.reset(q,d) | 若还传递了参数d,将会调用d而不是delete来释放q |
注意: 尽量不要使用get初始化另一个智能指针或者为智能指针赋值,因为万一delete的话容易出错
shared_ptr<int>p(new int(42));
int* q = p.get();
cout << "count:" << p.use_count() << *q << endl;
// delete q; //error:
{
auto t = shared_ptr<int>(q); //转换
cout << "count:" << t.use_count() << endl;
}
int nVal = *p;
cout << nVal << endl;
if(!p.unique())
p.reset(new string(*p)); //我们不是唯一的用户;分配新的拷贝
*p+=newVal; //可以改变对象的值
5. 自定义删除器
struct destination {
}; //正在连接什么
struct connection {
};
class test2 {
public:
connection& connect(connection* s)
{
cout << "正在连接..." << endl;
s = new connection();
return *s;
}
void static disconnect(connection* s)
{
cout << "正在断开连接..." << endl;
}
void test()
{
connection p;
connection* d = new connection();
p = connect(d);
//shared_ptr<connection>sp(&p,disconnect);
//error:lambda代表了删除函数。那么参数列表也要和删除函数一致,因为delete内部是free(p)。
//shared_ptr<connection>sp(&p, [&p] { disconnect(&p); });
shared_ptr<connection>sp(&p, [](connection* s) {
disconnect(s); });
}
};