为什么我应该使用 DECLARE_DYNAMIC 而不是 DECLARE_DYNCREATE?
DECLARE_DYNCREATE 提供与 DECLARE_DYNAMIC 完全相同的功能以及动态对象创建能力.那么为什么有人应该使用 DECLARE_DYNAMIC 而不是 DECLARE_DYNCREATE?
DECLARE_DYNCREATE provides exactly the same feature of DECLARE_DYNAMIC along with its dynamic object creation ability. Then why should anyone use DECLARE_DYNAMIC instead of DECLARE_DYNCREATE?
推荐答案
记录宏以提供不同的功能.
The macros are documented to provide different functionality.
DECLARE_DYNAMIC
:
在从 CObject 派生类时,添加了访问有关对象类的运行时信息的功能.
Adds the ability to access run-time information about an object's class when deriving a class from CObject.
这提供了自省功能,类似于 RTTI (运行时类型信息) 由 C++ 提供.应用程序可以通过关联的 CObject 派生类实例的运行时类型" rel="noreferrer">CRuntimeClass
结构.它在您需要检查对象是否属于特定类型或具有特定基类类型的情况下很有用.CObject::IsKindOf
中的示例应该给你一个好主意.
This provides the functionality for introspection, similar to RTTI (Run-Time Type Information) provided by C++. An application can query a CObject
-derived class instance for its run-time type through the associated CRuntimeClass
Structure. It is useful in situations where you need to check that an object is of a particular type, or has a specific base class type. The examples at CObject::IsKindOf
should give you a good idea.
DECLARE_DYNCREATE
:
使 CObject 派生类的对象能够在运行时动态创建.
Enables objects of CObject-derived classes to be created dynamically at run time.
此宏允许在运行时动态创建类实例.该功能是通过类工厂方法 CRuntimeClass::CreateObject
.当您需要在运行时根据类类型的字符串表示创建类实例时,可以使用它.一个示例是可自定义的 GUI,它是从初始化文件构建的.
This macro enables dynamic creation of class instances at run-time. The functionality is provided through the class factory method CRuntimeClass::CreateObject
. It can be used when you need to create class instances at run-time based on the class type's string representation. An example would be a customizable GUI, that is built from an initialization file.
这两个功能都是通过相同的 CRuntimeClass
结构实现的,这可能会导致它们可以互换使用的结论.事实上,使用不适当宏的代码将编译得很好,并公开所需的运行时行为.区别纯粹是语义上的:宏传达不同的意图,应根据所需的功能使用,以传达开发者的意图.
Both features are implemented through the same CRuntimeClass
Structure, which may lead to the conclusion that they can be used interchangeably. In fact, code that uses an inappropriate macro will compile just fine, and expose the desired run-time behavior. The difference is purely semantic: The macros convey different intentions, and should be used according to the desired features, to communicate developer intent.
还有第三个相关宏,DECLARE_SERIAL
:
There's also a third related macro, DECLARE_SERIAL
:
生成可序列化的CObject派生类所需的 C++ 标头代码.
Generates the C++ header code necessary for a CObject-derived class that can be serialized.
它支持对各个 CObject
派生类实例进行序列化,例如到文件、内存流或网络套接字.由于反序列化过程需要从序列化流中动态创建对象,因此它包含 DECLARE_DYNCREATE
的功能.
It enables serialization of respective CObject
-derived class instances, for example to a file, memory stream, or network socket. Since the deserialization process requires dynamic creation of objects from the serialized stream, it includes the functionality of DECLARE_DYNCREATE
.
综合起来,以下列表应该可以帮助您为特定场景选择正确的宏:
Put together, the following list should help you pick the right macro for your specific scenarios:
- 如果您的代码需要检索对象的运行时类型,请使用
DECLARE_DYNAMIC
. - 如果需要根据类型的字符串表示动态创建类实例,请使用
DECLARE_DYNCREATE
. - 如果您还需要提供序列化支持,请使用
DECLARE_SERIAL
.
相关文章