vendredi 28 juin 2019

If I want to transfer a resource, which kind of parameter type should I choose? weak_ptr? or shared_ptr

There is a resource: "aaa", who's type is "AAA". "aaa" is managed by shared_ptr, and we can get it's weak_ptr by function: get_aaa_weakPtr()

Now, the client_code want to visit "aaa", and transfer "aaa" to it's child_functions. Just like this:

void mainFunc(){

std::shared_ptr<A> sPtr = get_aaa_weakPtr().lock();
assert( sPtr );
// use sPtr
// ...

childFuncA( /* also_need_aaa */ );
childFuncB( /* also_need_aaa */ );
childFuncC( /* also_need_aaa */ );
}

which kind of parameter type should I choose in child_functions? weak_ptr? or shared_ptr?

Aucun commentaire:

Enregistrer un commentaire