小弟最近拜读了大名鼎鼎的《深入浅出MFC》,有一事不解:在第三章中,为什么具有动态创建能力的类只有CFrameWnd和CWnd,而其他类如CWinApp,CView等都不具有这一性质呢?还望高手解惑!

解决方案 »

  1.   

    是呀!他时指RUNTIMECLASS的!
      

  2.   

    dycdyc123(重出江湖):在mdi中,有时一个文档类可以被多个框架使用,那么这个文档的实例是不是创建了多个?这如何解释?
      

  3.   

    CView::OnDraw是纯虚函数,你怎么去动态创建一个CView对象。至于CWinApp,它有动态创建的必要吗?
      

  4.   

    并不会去创建CView对象,这我是知道的。
    969209pxb(power):难道真是这样吗?
    在mdi中,将CView的继承类CYourView变为类链表,难道这不是典型的内部动态创建吗?
      

  5.   

    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
      

  6.   

    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此宏只能说明在运行时刻你的对象的访问方式,并没有说明能否动态创建。请高手指点。
      

  7.   

    CWinApp 是一个程序的主要入口.CView也属于动态创建,如果不是,怎么可能用IsKindof这样的函数呢,动态创建主要用来建立一张类的链表
      

  8.   

    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的原因。