Practice mode C++

What happens when dynamic_cast<Derived*>(basePtr) is applied and basePtr does not actually point to a Derived object?

Correct answer B. It returns a null pointer

Explanation

dynamic_cast checks the object's dynamic type at run time and requires a polymorphic base class. For pointer targets, a failed cast returns a null pointer, so code typically writes if (auto d = dynamic_cast(p)). For reference targets there is no null reference, so a failed cast throws std::bad_cast instead.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.