方法1:
    CMyClass *pClass=(CMyClass *)RUNTIME_CLASS(CMyClass )->CreateObject();方法2:
    CMyClass *pClass=new CMyClass ;    这两种方式产生的对象有区别吗??我看RUNTIME_CLASS的源代码,CreateObject()最后调用的也是new 来分配的,那要是这两种没区别,那第一种我产生pClass后是不是还要delete??谢谢回答!!

解决方案 »

  1.   

    createobject 的实现和new不一样,好像是从线程局部存储中创建,程序退出自动释放, 而new ,需要手动delete。 具体可以看看createobject的实现。
      

  2.   

    CreateObject的实现在mfc\src\OBJCORE.CPP 文件中。
      

  3.   

    谢谢西瓜,“线程局部存储中创建”有点抽象,是不是就相当于static,但是《深入浅出MFC》说是调用了new来创建的实例,但是不知道创建后怎么释放的!!
      

  4.   

    “线程局部存储中创建” 和 static 是两个概念,可以google一下。
      

  5.   

    看看这个,
    CreateObject就是new操作,之所以这样用只不过是为了根据识别的类型去动态创建对象。
    #define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
    CObject* PASCAL class_name::CreateObject() \
    { return new class_name; } \
    IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
    class_name::CreateObject)
      

  6.   

    new 出来的要 delete
    create好像不要吧!
      

  7.   

    晕,来了一位,那我问问方雨,我还需要delete吗??
      

  8.   


    CObject* CRuntimeClass::CreateObject()
    {
    if (m_pfnCreateObject == NULL)
    {
    TRACE(_T("Error: Trying to create object which is not ")
      _T("DECLARE_DYNCREATE \nor DECLARE_SERIAL: %hs.\n"),
    m_lpszClassName);
    return NULL;
    } CObject* pObject = NULL;
    TRY
    {
    pObject = (*m_pfnCreateObject)();//这个地方调用类自己的CreateObject函数
    }
    END_TRY return pObject;
    }
      

  9.   

    需要delete,不过,mfc的view/doc框架已经自动去完成了。
      

  10.   


    void CDocument::OnCloseDocument()
    // must close all views now (no prompting) - usually destroys this
    {
    // destroy all frames viewing this document
    // the last destroy may destroy us
    BOOL bAutoDelete = m_bAutoDelete;
    m_bAutoDelete = FALSE;  // don't destroy document while closing views
    while (!m_viewList.IsEmpty())
    {
    // get frame attached to the view
    CView* pView = (CView*)m_viewList.GetHead();
    ASSERT_VALID(pView);
    CFrameWnd* pFrame = pView->GetParentFrame();
    ASSERT_VALID(pFrame); // and close it
    PreCloseFrame(pFrame);
    pFrame->DestroyWindow();
    // will destroy the view as well
    }
    m_bAutoDelete = bAutoDelete; // clean up contents of document before destroying the document itself
    DeleteContents(); // delete the document if necessary
    if (m_bAutoDelete)
    delete this;//这里调用了delete
    }
      

  11.   

    恩,我知道,(*m_pfnCreateObject)()就是指向了每个类中的一个函数createObject(),但是createObject()函数return的是new CMyClass,所以归根结底RUNTIME_CLASS(CMyClass )->CreateObject(); 还是调用的new,就是不知道是不是要自己动手delete。
      

  12.   


    // self destruction
    void CView::PostNcDestroy()
    {
    // default for views is to allocate them on the heap
    //  the default post-cleanup is to 'delete this'.
    //  never explicitly call 'delete' on a view
    delete this;//这里
    }
    void CFrameWnd::PostNcDestroy()
    {
    // default for frame windows is to allocate them on the heap
    //  the default post-cleanup is to 'delete this'.
    // never explicitly call 'delete' on a CFrameWnd, use DestroyWindow instead
    delete this;//这里
    }
      

  13.   

    谢谢方雨,不过你好像没说到点子上,你那个delete this是删除的pDocTemplate = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CClientOPCDoc),
    RUNTIME_CLASS(CMainFrame),      
    RUNTIME_CLASS(CClientOPCView));
    AddDocTemplate(pDocTemplate);中的RUNTIME_CLASS(CClientOPCDoc)产生的doc对象,我现在问的是我自己RUNTIME_CLASS出来的一个CRuntimeClass产生的对象。算了,我自己找找吧,明天我加点分,再给你们结贴!!
      

  14.   

    new出来的东西,不用的时候就要delete,这可以说是C++铁的定律,呵呵
      

  15.   

    CObject类(以及支持动态创建的继承类)的构造函数和析构函数都是保护的,所以不能在外部通过new/delete来分配、析构,你看看向导生成的文档/视图类等,因为这类对象在框架中已经有自己的分配和销毁途径,不希望用户自己去用new分配。比如视图类,在OnNcDestroy()中有delete this删除自身的代码。这种设计是一种策略,如果你看看设计模式或者Effective C++等深入讲解设计方法和语法的书,有助于你的理解。
      

  16.   

    我错了,没看m_pfnCreateObject的初始化。
    #define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
    CObject* PASCAL class_name::CreateObject() \
    { return new class_name; } \
    IMPLEMENT_RUNTIMECLASS(class_name, base_class_name, 0xFFFF, \
    class_name::CreateObject, NULL)以为在其它地方赋的值。
      

  17.   


    文档视图结构中,下面这几句能理解,就说明明白了:
    protected: // 仅从序列化创建
    CxxxDoc();
    DECLARE_DYNCREATE(CxxxDoc)protected: // 仅从序列化创建
    CxxxView();
    DECLARE_DYNCREATE(CxxxView)