MFC DYNAMIC_DOWNCAST vs. dynamic_cast

天大地大妈咪最大 提交于 2019-12-24 03:02:28

问题


What is the difference between DYNAMIC_DOWNCAST from MFC library and standard C++ dynamic_cast operator? Can I use safety dynamic_cast instead of DYNAMIC_DOWNCAST for MFC objects?

When my classes contain DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros, can I use dynamic_cast operator or I must use DYNAMIC_DOWNCAST macro for this type of objects?


回答1:


What is the difference between DYNAMIC_DOWNCAST from MFC library and standard C++ dynamic_cast operator?

DYNAMIC_DOWNCAST and dynamic_cast achieve the same thing, information about an object's data type at runtime, through different mechanisms. DYNAMIC_DOWNCAST works through the use of a set of macros to declare and implement methods used to get the object's class information. dynamic_cast achieves the same thing through the use of Run-Time Type Information, which is implemented by the compiler.

Can I use safety dynamic_cast instead of DYNAMIC_DOWNCAST for MFC objects? When my classes contain DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros, can I use dynamic_cast operator or I must use DYNAMIC_DOWNCAST macro for this type of objects?

The use of dynamic_cast is allowed if you compile using RTTI and have at least one virtual method. DYNAMIC_DOWNCAST implements a bunch of virtual methods so you only have to check if your compiler supports RTTI and that it is turned on.



来源:https://stackoverflow.com/questions/29030525/mfc-dynamic-downcast-vs-dynamic-cast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!