using boost::bind
in this fashion will copy the arguments ensuring shared_ptr<ClassB>
remains in scope. What you're doing is perfectly fine.
access to shared_ptr in boost dispatcher
-
28-09-2022 - |
Pregunta
I use a boost dispatcher (io_service) to execute asynchronously "methodB". Into this method, I would like to keep a pointer to the instance of the class B, so I use shared_ptr. But in the below example, I wonder whether after the scope of "methodA", the pointer will still be accessible into "methodB" or if the pointer refcounter will be equaled to zero.
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 ???
}
Thanks you very much.
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow