共用方式為


IMPLEMENT_DYNCREATE

Enables objects of CObject-derived classes to be created dynamically at run time when used with the DECLARE_DYNCREATE macro.

IMPLEMENT_DYNCREATE(class_name, base_class_name )

Parameters

  • class_name
    The actual name of the class.

  • base_class_name
    The actual name of the base class.

Remarks

The framework uses this ability to create new objects dynamically, for example, when it reads an object from disk during serialization. Add the IMPLEMENT_DYNCREATE macro in the class implementation file. For more information, see CObject Class Topics.

If you use the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE macros, you can then use the RUNTIME_CLASS macro and the CObject::IsKindOf member function to determine the class of your objects at run time.

If DECLARE_DYNCREATE is included in the class declaration, then IMPLEMENT_DYNCREATE must be included in the class implementation.

Note that this macro definition will invoke the default constructor for your class. If a non-trivial constructor is explicitly implemented by the class, it must also explicitly implement the default constructor as well. The default constructor can be added to the class's private or protected member sections to prevent it from being called from outside the class implementation.

Example

class CMyDynCreateObj : public CObject
{
     int m_Num;
public:
     DECLARE_DYNCREATE(CMyDynCreateObj)
     CMyDynCreateObj(int Num) { m_Num = Num; }
private:
     CMyDynCreateObj() { m_Num = 0; }  // provide default constructor only for 
                                       // dynamic creation 
};
IMPLEMENT_DYNCREATE(CMyDynCreateObj, CObject)

Requirements

Header: afx.h

See Also

Reference

DECLARE_DYNCREATE

RUNTIME_CLASS

CObject::IsKindOf

Concepts

MFC Macros and Globals