2014-01-16 75 views
0

我使用boost调度器(io_service)异步执行“methodB”。在这个方法中,我想保留一个指向B类实例的指针,所以我使用shared_ptr。但在下面的例子中,我想知道在“methodA”范围之后,指针仍然可以访问到“methodB”中,或者指针refcounter将等于零。在boost调度器中访问shared_ptr

void ClassA::methodA(){ 
    shared_ptr<ClassB> pointer(new ClassB); 
    m_dispatcher.post(boost::bind(&ClassA::methodB, this, pointer); // boost io_service 
} 

void ClassA::methodB(shared_ptr<ClassB> pointer){ 
    ClassB *p = pointer.get(); // access to pointer ??? 
} 

非常感谢。

回答

1

以这种方式使用boost::bind将复制确保shared_ptr<ClassB>保持在范围内的参数。你在做什么是完美的。

相关问题