Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart pointer question

I'm rewriting my code with smart pointer. I have such situation:

void Foo(SomeClass *p) { }
boost::shared_ptr<SomeClass> p(new SomeClass);

Now what to do: pass original pointer from wrapper (p.get()) or rewrite function argument and pass smart pointer directly like:

void Foo(boost::shared_ptr<Foo> obj) { }

I'm not sure. As I understand smart pointers should follow some pointer and look whether it still need in program. So we can pass original pointer.

like image 945
Max Frai Avatar asked Dec 28 '22 03:12

Max Frai


1 Answers

Unless Foo needs to take (shared) ownership of *p you should keep the signature the same and just pass p.get(). It's the simplest and most flexible option as well as requiring the least change to your existing code.

like image 200
CB Bailey Avatar answered Jan 09 '23 03:01

CB Bailey