class CWinApp : public CWinThread { DECLARE_DYNAMIC(CWinApp) public:// Constructor CWinApp(LPCTSTR lpszAppName = NULL); // app name defaults to EXE name//////////////////////////// #ifdef _AFXDLL class CView : public CWnd #else class AFX_NOVTABLE CView : public CWnd #endif { DECLARE_DYNAMIC(CView)// Constructors protected: CView();从源码知,DECLARE_DYNAMIC宏只是动态识别,要完成动态创建,必须是 DECLARE_DYNCREATE
source macro define:#define DECLARE_DYNAMIC(class_name) \ protected: \ static CRuntimeClass* PASCAL _GetBaseClass(); \ public: \ static const AFX_DATA CRuntimeClass class##class_name; \ virtual CRuntimeClass* GetRuntimeClass() const; \ msdn explain:If you use the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros as described, you can then use the RUNTIME_CLASS macro and the CObject::IsKindOf function to determine the class of your objects at run time.DECLARE_DYNAMIC此宏只能说明在运行时刻你的对象的访问方式,并没有说明能否动态创建。请高手指点。
the answer is:Classes derived from CObject can support dynamic creation, which is the ability to create an object of a specified class at run time. Document, view, and frame classes, for example, should support dynamic creation. The CreateObject member function can be used to implement this function and create objects for these classes during run time. For more information on dynamic creation and the CreateObject member, seeCObject Class Topics andCObject Class: Specifying Levels of Functionality in Visual C++ Programmer’s Guide. (copy msdn document)其实在run time条件下,无论document或view还是frame类对象都可以动态创建,这就是在mfc中有CRuntimeClass的原因。
969209pxb(power):难道真是这样吗?
在mdi中,将CView的继承类CYourView变为类链表,难道这不是典型的内部动态创建吗?
{
DECLARE_DYNAMIC(CWinApp)
public:// Constructor
CWinApp(LPCTSTR lpszAppName = NULL); // app name defaults to EXE name////////////////////////////
#ifdef _AFXDLL
class CView : public CWnd
#else
class AFX_NOVTABLE CView : public CWnd
#endif
{
DECLARE_DYNAMIC(CView)// Constructors
protected:
CView();从源码知,DECLARE_DYNAMIC宏只是动态识别,要完成动态创建,必须是 DECLARE_DYNCREATE
protected: \
static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
static const AFX_DATA CRuntimeClass class##class_name; \
virtual CRuntimeClass* GetRuntimeClass() const; \
msdn explain:If you use the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros as described, you can then use the RUNTIME_CLASS macro and the CObject::IsKindOf function to determine the class of your objects at run time.DECLARE_DYNAMIC此宏只能说明在运行时刻你的对象的访问方式,并没有说明能否动态创建。请高手指点。