且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MFC DYNAMIC_DOWNCAST与dynamic_cast

更新时间:2022-06-23 23:11:07

MFC库中的DYNAMIC_DOWNCAST与标准C ++ dynamic_cast运算符有什么区别?

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

DYNAMIC_DOWNCAST和dynamic_cast通过不同的机制在运行时实现了相同的功能,即有关对象数据类型的信息. DYNAMIC_DOWNCAST通过使用一组宏来声明和实现用于获取对象的类信息的方法. dynamic_cast通过使用由编译器实现的运行时类型信息来实现相同的目的.

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.

我可以对MFC对象使用安全dynamic_cast而不是DYNAMIC_DOWNCAST吗? 当我的类包含DECLARE_DYNAMIC和IMPLEMENT_DYNAMIC宏时,我可以使用dynamic_cast运算符,还是必须对此类对象使用DYNAMIC_DOWNCAST宏?

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?

如果使用RTTI进行编译并且至少具有一种虚拟方法,则允许使用dynamic_cast. DYNAMIC_DOWNCAST实现了许多虚拟方法,因此您只需要检查编译器是否支持RTTI并且已将其打开.

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.