I do not understand dynamic_cast.
I have been reading this tutorial and I can not get the hang of it. http://www.cplusplus.com/doc/tutorial/typecasting/
I Keep asking myself:
#1 Why do I need dynamic_cast
#2 What does it do. (I know it converts pointers to another type pointer but can't you do that already?)
Any explanations would help A LOT.
What helped you learn dynamic_cast?
Thank You for your time. :)
The purpose of both static_cast and dynamic_cast is to support safe up casting (and down casting) for polymorphic types.
The static_cast will always perform the cast; it trusts you to give it a valid pointer.
The dynamic_cast uses RTTI to check that the case it valid for a given pointer. If it's not valid, it returns a null pointer.
Both can also be used with references; as you can't return a "null ref" (?), an attempt at an invalid dynamic cast causes a bad_cast exception to be raised.
No, it's to access a method which is declared in in a derived class, but not the base. But you can also use it to make it clear that you mean to down cast, though it's not actually needed that direction.
Ohhhhhhhhhhhhhhh So dynamic_cast ensures that the conversion is a valid object. And it is used in abstract classes as well. If it fails it gives you a NULL pointer.
I get it!!!! I got it!!! Thank you Thank You Thank You Thank You Thank You Thank You Thank You
:)