What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type?
I generally pass shared_ptrs by reference to avoid a needless copy:
int foo(const shared_ptr<bar>& ptr); but this doesn't work if I try to do something like
int foo(const shared_ptr<Base>& ptr); ... shared_ptr<Derived> bar = make_shared<Derived>(); foo(bar); I could use
foo(dynamic_pointer_cast<Base, Derived>(bar)); but this seems sub-optimal for two reasons:
- A
dynamic_castseems a bit excessive for a simple derived-to-base cast. - As I understand it,
dynamic_pointer_castcreates a copy (albeit a temporary one) of the pointer to pass to the function.
Is there a better solution?
Update for posterity:
It turned out to be an issue of a missing header file. Also, what I was trying to do here is considered an antipattern. Generally,
Functions that don't impact an object's lifetime (i.e. the object remains valid for the duration of the function) should take a plain reference or pointer, e.g.
int foo(bar& b).Functions that consume an object (i.e. are the final users of a given object) should take a
unique_ptrby value, e.g.int foo(unique_ptr<bar> b). Callers shouldstd::movethe value into the function.Functions that extend the lifetime of an object should take a
shared_ptrby value, e.g.int foo(shared_ptr<bar> b). The usual advice to avoid circular references applies.
See Herb Sutter's Back to Basics talk for details.
shared_ptr? Why no const-reference of bar?dynamiccast is only needed for downcasting. Also, passing the derived pointer should work just fine. It'll create a newshared_ptrwith the same refcount (and increase it) and a pointer to the base, which then binds to the const reference. Since you're already taking a reference, however, I don't see why you want to take ashared_ptrat all. Take aBase const&and callfoo(*bar).foo(bar)) doesn't work, at least in MSVC 2010.shared_ptrto pass to the function? I'm fairly sure there's no way to avoid that.