make_shared
不支持自定义删除器shared_ptr<Bar> sp2(sp1, deleter1);
无法指定自定义删除器?- 《C++ Primer 5th》P413 错误?
- sp1 不能是智能指针,可以是内置指针
- reset 参数为内置指针,可以重新指定删除器
测试代码
class Bar {
public:
Bar(int ii) : i(ii) { cout << "Bar(" << i << ")" << endl; }
~Bar() { cout << "~Bar(" << i << ")" << endl; }
int i;
};
void deleter1(Bar* b) { cout << "deleter1 (" << b->i << ")" << endl; }
int main() {
shared_ptr<Bar> sp1(new Bar(1), deleter1);
auto sp2 = sp1; // OK
// shared_ptr<Bar> sp2(sp1, deleter1); // NOK
cout << endl;
sp1.reset(new Bar(2));
// 先析构 sp2--> Bar1 ,再析构 sp1 --> Bar2
}
运行结果
Bar(1)
Bar(2)
deleter1 (1)
~Bar(2)
标签:Bar,自定义,sp1,deleter1,shared,ptr,deleter
From: https://www.cnblogs.com/tengzijian/p/17289473.html