I was reading about dynamic_pointer_cast. I found that we can dynamically upcast or downcast the shared pointer of one type to another at run time but i did't understand the concept completely. Shall both the classes must have same properties? If the properties must be same then what is need of dynamic casting?
Asked
Active
Viewed 2,771 times
2
-
*Shall both the classes must have same properties?* It's no clear to me what you mean by that. – R Sahu Feb 25 '18 at 07:31
-
What i mean is that if we are type casting shared pointer to one class to shared pointer of another class, Should those two classes must have same variables defined in them? – A.Saroj Feb 25 '18 at 07:45
-
@RSahu can you explain me the concept of dynamic pointer casting? like when is it used and how? – A.Saroj Feb 25 '18 at 07:48
-
3A dynamic pointer cast essentially applies a `dynamic_cast` on the raw pointer held within. If you don't know what a `dynamic_cast` is or what it's good for, you need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), not Stack Overflow. – StoryTeller - Unslander Monica Feb 25 '18 at 08:28
-
*"When is it used?"* - Hardly ever. *"What are the alternatives?"* - None. If you need it, you need it. That's why it is there. You can probably leave it for now and come back to this should you ever need to transform one type of shared_ptr into another. – Bo Persson Feb 25 '18 at 08:58
-
@BoPersson suppose i am creating a shared pointer of base class type using dynamic_pointer_cast from shared pointer of derived class type. Can i call base class member function using casted shared pointer? – A.Saroj Feb 25 '18 at 09:09
-
@A.Saroj - Yes, but you can calls base class members on the derived class as well. Dynamic cast is for when you have a pointer to Base, which may or may not actually point to a Derived. Then you can get a pointer to Derived, if it actually is that, or an empty pointer if otherwise. (I have never seen a case where this was actually useful. Most often you call virtual functions on the Base pointer and it works without a cast). – Bo Persson Feb 25 '18 at 09:26
-
@BoPersson It is useful in cases where the class API is not completely polymorphic and calling code needs to act differently based on the concrete type of the class. Some would classify this as a design smell. – Rotem Feb 25 '18 at 09:32
1 Answers
2
dynamic_pointer_cast
is used to convert std::shared_ptr
type, e.g. from the pointer on a base class to a pointer on a derived class:
#include <memory>
struct A{ virtual ~A() = default; };
struct B: A {};
int main()
{
std::shared_ptr<A> pA = std::make_shared<B>();
std::shared_ptr<B> pB = std::dynamic_pointer_cast<B>(pA);
}
So dynamic_pointer_cast
is a convenience wrapper around dynamic_cast
:
template< class T, class U >
std::shared_ptr<T> dynamic_pointer_cast( const std::shared_ptr<U>& r ) noexcept
{
if (auto p = dynamic_cast<typename std::shared_ptr<T>::element_type*>(r.get())) {
return std::shared_ptr<T>{r, p};
} else {
return std::shared_ptr<T>{};
}
}

Fedor
- 17,146
- 13
- 40
- 131