casting - Forced necessity of dynamic_cast operator in C++ -


dynamic_cast used when have base class pointer , want downcast derived class. instance,

class {       public:       virtual void foo(); };  class b : public  {       public:       void foo(); };  main() {     a* = new b();     b* b = dynamic_cast<b*> (a); } 

but, same can accomplised using c-style cast:

b* b = (b*)a; 

so, question circumstances/cases becomes necessary use operator i.e. there no other choice?

when don't know a points object of type b dynamic_cast return null pointer, can check , handle situation accordingly. c-style cast pointer, when try use undefined behavior happens.


Comments