Re: ref to shared_ptr?
On Oct 22, 4:04 pm, ".rhavin grobert" <cl...@yahoo.de> wrote:
are there any pro's and con's in using references to shared_ptr<>'s ?
example:
__________________________________________________
void obj::foo(shared_ptr<>); vs. void obj::foo(shared_ptr<>&);
Passing shared_ptr by value causes a call to shared_ptr's copy
constructor just before the function call and a call to the destructor
upon leaving the function. On the other hand, passing by reference
does not involve any constructor/destructor calls (unless it is a
reference to const bound to a temporary object). It depends on an
application whether the difference is noticeable.
Speaking generally, functions that do not acquire or share the
ownership of passed objects should not be taking any smart pointers,
rather they should accept objects by plain reference/pointer.
and
shared_ptr<>& obj::foo2(); vs. shared_ptr<> obj::foo2();
Performance considerations are the same as for passing shared_ptr to
functions.
Again, one should really think about interfaces and ownership. Is it
expected that the callers of obj::foo2() are going to share the
ownership of the object. If not, it is better to return a plain
pointer/reference.
--
Max