1. 使用场景
p.reset() 若p是唯一指向其对象的shared_ptr,reset会释放此对象。
p.reset(q) 释放原有对象的同时,若传递了可选的内置参数指针q,会令p指向q,否则会将p置为空
p.reset(q,d) 若还传递了参数d,将会调用d而不是delete来释放q
1.1 reset函数定义
void reset() noexcept {
// release resource and convert to empty shared_ptr object
shared_ptr().swap(*this);
}
template <class _Ux>
void reset(_Ux* _Px) {
// release, take ownership of _Px
shared_ptr(_Px).swap(*this);
}
template <class _Ux, class _Dx>
void reset(_Ux* _Px, _Dx _Dt) {
// release, take ownership of _Px, with deleter _Dt
shared_ptr(_Px, _Dt).swap(*this);
}
template <class _Ux, class _Dx, class _Alloc>
void reset(_Ux* _Px, _Dx _Dt, _Alloc _Ax) {
// release, take ownership of _Px, with deleter _Dt, allocator _Ax
shared_ptr(_Px, _Dt, _Ax).swap(*this);
}
1.2 swap函数定义
void swap(shared_ptr& _Other) noexcept {
this->_Swap(_Other);
}