S
S
sabn1k2016-02-26 18:29:00
Programming
sabn1k, 2016-02-26 18:29:00

How to use dynamic_cast?

The book is not clear enough.
What is dynamic_cast for and how to use it? Can you explain in simple terms?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-02-26
@MiiNiPaa

To safely cast a base class to a child.
Let's say you have a Base* foo pointer, which most likely points to a derived class. And you want to cast it to a derivative in order to use its functions. But there is a chance that it is not a derived class in which case you will have problems.
To avoid problems you can use dynamic_cast(foo). The cast will return nullptr if the pointer does not point to the Derived class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question