Difference in behavior while using dynamic_cast with reference and pointers

后端 未结 4 994
小鲜肉
小鲜肉 2020-11-29 04:23

I was checking the behavior of dynamic_cast and found that when it fails, std::bad_cast exception is thrown only if the destination is a reference type. If the destination i

相关标签:
4条回答
  • 2020-11-29 04:52

    Yes, this is correct behaviour. The reason is that you can have a null pointer, but not a null reference - any reference has to be bound to an object.

    So when dynamic_cast for a pointer type fails it returns a null pointer and the caller can check for that, but when it fails for a reference type it can't return a null reference, so an exception is the only reasonable way to signal a problem.

    0 讨论(0)
  • 2020-11-29 05:01

    Yes, it is. Because dynamic_cast can't return NULL for a failed reference cast, an exception is the only way out.

    That is, a reference can't be NULL, so there is nothing suitable to return.

    0 讨论(0)
  • 2020-11-29 05:02

    See the C++ Standard, section 5.2.7/9:

    9 The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws bad_cast (18.5.2).

    As to why - these are Stroustrup's words from the D & E book, section 14.2.2:

    I use a reference cast when I want an assumption about a reference type checked and consider it a failure for my assumption to be wrong. If instead I want to select among plausible alternatives, I use a pointer cast and test the result.

    0 讨论(0)
  • 2020-11-29 05:02

    Yes, 5.2.7/9

    The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws bad_cast (18.5.2).

    0 讨论(0)
提交回复
热议问题