Suppose I have a class C
, inheriting from B
, itself virtually inheriting from A
:
class A {};
class B : virtual public A {};
class C : public B {};
Now suppose I have a std::shared_ptr<A>
managing an object which I know for sure is of some class inheriting B
(e.g., of class C
), and I even happen to have it available as a raw B
pointer, in a way equivalent to the minimal example below:
B * pb = new C;
std::shared_ptr<A> spa(pb);
Now I want to downcast the std::shared_ptr<A>
to a std::shared_ptr<B>
. I can't use static_pointer_cast
because the inheritance is virtual. I could use dynamic_pointer_cast
but it seems an overkill given that I already have the object available as a raw B
pointer.
Is the following code the appropriate way to achieve my goal?
std::shared_ptr<B> spb(spa, pb);
It seems that yes, but I'd like some confirmation from someone who knows better (I never had to use the aliasing constructor before).