我最近在看侯俊杰《深入浅出MFC》,里面类库设计例如:
  Cdocument* Cdoctemplate::createnewDocument()
{
    CDoument* pDocument=(CDocument*)m_pDocClass->CreateObject();
  我的问题是;这样的指针指象何处?类里没产生任何对象呀?在许多类的定义中都有这问题!

解决方案 »

  1.   

    详细一点地说是:CreateObject()在堆上生成了一个CDocument型的对象,并返回其指针.
      

  2.   

    Zark(金陵五月) 老兄你好啊,好久不见。CreateObject()创建了这个对象。
      

  3.   

    m_pDocClass是一个CRuntimeClass的指针,而CRuntimeClass有一个static成员函数CreateObject()来创建它所代表的对象。
      

  4.   

    CreateObject()好象是每个类都有!并非CRuntimeClass类的成员函数!比如:Cobject* PASCAL CFrameWnd::CreateObject()
       { return new CframeWnd;}
    m_pDocClass是一个CRuntimeClass的指针,(CDocument*)m_pDocClass怎么能指向一个并非CRuntimeClass类的函数,
      

  5.   

    Rigel(猎户座-参宿七): 别来无恙? 年终修假了几天.
      

  6.   

    CreateObject()并不是每个都有的,只有从CObject类衍生下来的类才有.但CreateObject()不是CObject类的成员函数,而是CRuntimeClass类的成员函数,CRuntimeClass类没有基类,它是个祖宗.由于每个CObject类中都带有一个CRuntimeClass类成员,所以才出现你所见的情况.
      

  7.   

    Zark!谢谢你!我还是抄一下书中的代码吧!
    class cframewnd:public cwnd
    {
       public:
           static cruntimeclass classcframewnd;
           virtual cruntimeclass getruntimeclass() const;
           static cobject *pascal createobject();
       ...
    };
    cobject *pascal cframewnd::createobject()
       {return new cframewnd;}
    明明是类里的函数!再说如果createobject()定义在cruntimeclass里边,
    又怎么能对不同类new 呢?除非用switch/case选择!在说根据C++规则自己的问题自己解决,最好也是类自己解决该类对象动态创建的问题!
      

  8.   

    你所写的cframewnd是MFC中CFrameWnd类吗?我查了MSDN没有发现你所写的代码及声明.如果这个类你自创的,那么如何实现动态创建的问题则是属于你自己的范畴.我还以为你是在讨论MFC中实现方式呢!
      

  9.   

    zark!我没有看MSDN,但侯捷在讲动态创建时是这么做的!如果不这么做!那实际又怎么做的呢?
      

  10.   

    在MFC中是通过两个宏来实现的.DECLARE_DYNMIC和IMPLEMENT_DYMIC,抄一个宏做说明,来自..\VC98\MFC\include\AFX.H#ifdef _AFXDLL
    #define DECLARE_DYNAMIC(class_name) \
    protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
    public: \
    static const AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \#define _DECLARE_DYNAMIC(class_name) \
    protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
    public: \
    static AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \#else
    #define DECLARE_DYNAMIC(class_name) \
    public: \
    static const AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \#define _DECLARE_DYNAMIC(class_name) \
    public: \
    static AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \#endif没有看过候捷的书,也不知他老人家到底说了什么,莫非那个cframewnd类是第老人家的杰作?
      

  11.   

    zark!这定义我也看过!你这里并没有提到 dynamic_create!如果每个类里都有这个宏,对于createobject(),一种情况就是每个类里声明一个 classruntime对象,createobject()为其成员函数,可这样一来怎么
    new ***(类名)呢?另一种情况就是每个类里有一个createobject(),它的调用靠classruntime对象里一个指向该类createobject()的指针!这样createobject()就可以这样实现:
      { new ***(该类名);}我看后一种比较合理!
    问一个问题!————
        DECLARE—DYNCREATE在头文件类声明里申明,而IMPLIMENT—DYNCREATE
    在.cpp文件里定义,他的作用就是生成一个classruntime对象的串表,问题是
      

  12.   

    接上————
        when?how 生成这串表?程序中没体现这样的调用?类的定义如过没声明对象也不会分配存储空间吧/
       zark!对你的不厌其烦的答复我非常感谢!我是山东大学学通信的一名学生!真希望认识你!by the way,MFC 的组织真是太乱了!
      

  13.   

    你理解错了,CRuntimeClass是为了在RUN-TIME动态生成对象而设立的;同时,你也理解对了,对于new xxxx来说是用不到这两个宏的.抄一下MSDN部分.
    CObject* CreateObject( );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.