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(5)); 
    //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(10)); 
    std::cout << "*sp == " << std::boolalpha 
        << *sp << std::endl; 

    sp.reset(new int(15), deleter()); 
    std::cout << "*sp == " << std::boolalpha 
        << *sp << std::endl; 
    getchar();
    return (0); 
} 

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

posted @ 2014-04-07 21:19  zzyoucan  阅读(1056)  评论(0编辑  收藏  举报