shared_ptr<> reset

时间:2023-03-09 15:29:02
shared_ptr<>   reset
// std_tr1__memory__shared_ptr_reset.cpp
// compile with: /EHsc
#include <memory>
#include <iostream> struct deleter
{
void operator()(int *p)
{
delete p;
}
}; int main()
{
std::shared_ptr<int> sp(new int());
//std::boolalpha bool类型输出为true或者false而不是0或者1,默认输出0和1
//reset更换管理对象
std::cout << "*sp == " << std::boolalpha
<< *sp << std::endl; sp.reset();
std::cout << "(bool)sp == " << std::boolalpha
<< (bool)sp << std::endl; sp.reset(new int());
std::cout << "*sp == " << std::boolalpha
<< *sp << std::endl; sp.reset(new int(), deleter());
std::cout << "*sp == " << std::boolalpha
<< *sp << std::endl;
getchar();
return ();
}

reset用处很多,其中的原理啥的还需探究