jeudi 5 septembre 2019

Appending an existing shared_ptr to a vector of shared_ptr

I have an existing vector of shared_ptr. I want to search through that vector and if a condition is met, copy the respective shared_ptr to a new vector.

...
//vector< shared_ptr<Foo> > main_vec; // which already has some data
vector< shared_ptr<Foo> > output_vec{};

for ( auto iter = main_vec.begin() ; iter != main_vec.end() ; ++iter )
{
  if ( (*iter)->bar() == true )
    output_vec.push_back( *iter );
}

return output_vec;
...

I am not convinced the above is correct?? I am guessing that this will copy the shared_ptr but not increase the ref_count of the original, or am I over thinking this? I'm pretty new to smart pointer.

TIA

Aucun commentaire:

Enregistrer un commentaire