C++ casting programmatically : can it be done?

后端 未结 8 444
你的背包
你的背包 2021-01-13 06:18

Let\'s say I have a Base class and several Derived classes. Is there any way to cast an object to one of the derived classes without the ne

8条回答
  •  逝去的感伤
    2021-01-13 06:36

    Your example won't port, because the exact format of name() isn't specified. You could try a succession of dynamic_casts. Dynamic_cast returns a null pointer if you cast to the wrong type. However, if you're doing a typeswitch like this one, there's something wrong with your design.

提交回复
热议问题