What is the advantage of using dynamic_cast instead of conventional polymorphism?

前端 未结 5 1859
青春惊慌失措
青春惊慌失措 2020-12-18 04:19

We can use Polymorphism (inheritance + virtual functions) in order to generalize different types under a common base-type, and then refer to different objects as if they wer

5条回答
  •  一整个雨季
    2020-12-18 05:04

    dynamic_cast needs to be used on base class pointer for down cast when member function is not available in base class, but only in derived class. There is no advantage to use it. It is a way to safely down cast when virtual function is not overridden from base class. Check for null pointer on return value. You are correct in that it is used where there is no virtual function derivation.

提交回复
热议问题