Trusted Versus Checked

struct Animal { virtual ~Animal() = default; };
struct Dog : Animal {};
struct Cat : Animal {};

Animal *a = new Cat();
Dog *d1 = static_cast<Dog *>(a);
Dog *d2 = dynamic_cast<Dog *>(a);

Which statement is true?

A

d2 throws std::bad_cast, which is how a failed downcast is reported.

B

d1 and d2 are both null: static_cast performs the same base-to-derived check and yields null when it fails.

C

d2 is null. d1 is non-null and does not point to a Dog, so any use of it is undefined behaviour.

D

d1 fails to compile, because Dog is not a base of Cat and the compiler can see the actual dynamic type here.