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

解决方案 »

  1.   

    CWinApp其基类是CWinThread,系统只能创建一个主线程main thread,所以不能再动态创建。
    而CView的却是CWnd,可以在动态创建。
      

  2.   

    补充:CYourWinApp是基于CWinApp的唯一的一个界面主线程。
      

  3.   

    你可以看看该书的圖8-1 Document/View/Frame 的產生
      

  4.   

    CView具有纯虚函数,当然不能够创建实例
    派生自CView的类就可以动态创建了
      

  5.   

    楼上的,我明白你的意思,但楼主好象不是这样说的,参考msdn 文档:Each application that uses the Microsoft Foundation classes can only contain one object derived from CWinApp. This object is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library. Declare your derived CWinApp object at the global level.
      

  6.   

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

  7.   

    CRuntimeClass 支持动态创建。
      

  8.   

    只要在派生类的声明中定义三个DECLARE宏中的一个,派生类就支持动态创建。
      

  9.   

    在第六章“从生到死”,如果你仔细领会,就会明百的。cwinapp 为什么只能有一个实例,而不能动态创建?它的作用是什么?frame view 为什么可以创建,它们的作用又是什么?仔细的看,上面说的很仔细的。个人建议,没写过sdk,不要读这本书。 因为这本书就是分析mfc如何包装
    sdk的,尤其是第三章,也是本书最精华的地方。 第六章,如果你有
    winmain() wndproc()的sdk经验,你会读的津津有味。
      

  10.   

    说实话,我买了这本书但只草草看了一遍,觉得实用意义不是很大,并不是我的水平很高,只是觉得就是完全了解所有细节又有什么意义?这本书改名叫做<如何编写一套自己的程序框架>可能更合适吧