Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use dynamic_cast of reference? [duplicate]

Tags:

c++

I am reading the book, "A Tour of C++", and cannot understand following paragraph. What does "a different type is unacceptable" mean? So, when to use pointer casting and when to use reference casting? Can somebody elaborate this? Thanks.

Edit: The other question, "Difference in behavior while using dynamic_cast with reference and pointers" is asking the behavior of dynamic_cast, which I could understand - return nullptr or throw exception. In this question, I am asking when to use one and when to use the other.

"We use dynamic_cast to a pointer type when a pointer to an object of a different derived class is a valid argument. We then test whether the result is nullptr. This test can often conveniently be placed in the initialization of a variable in a condition. When a different type is unacceptable, we can simply dynamic_cast to a reference type. If the object is not of the expected type, bad_cast is thrown:" - A Tour of C++, Section 4.5.3

like image 674
MaxHeap Avatar asked Nov 05 '15 21:11

MaxHeap


1 Answers

Basically if our object is allowed to be one of different types, we can dynamic_cast to a pointer so we can check if the cast succeeded:

void do_if_derived(Base& b) {
    Derived* d = dynamic_cast<Derived*>(&b);
    if (d) {
        // do something
    }
    else {
        // not a Derived, this is OK
    }
}

but if our object has to be a single specific type, we can dynamic_cast to a reference and let the cast throw if it happens to be wrong:

void this_better_be_a_derived(Base& b)
{
    Derived& d = dynamic_cast<Derived&>(b);
    // do stuff with d
    // will throw if, e.g. b is a DifferentDerived& instead
}

It's a matter of wanting to handle the failure case via a branch or via an exception.

like image 152
Barry Avatar answered Sep 17 '22 13:09

Barry