方法1:
CMyClass *pClass=(CMyClass *)RUNTIME_CLASS(CMyClass )->CreateObject();方法2:
CMyClass *pClass=new CMyClass ; 这两种方式产生的对象有区别吗??我看RUNTIME_CLASS的源代码,CreateObject()最后调用的也是new 来分配的,那要是这两种没区别,那第一种我产生pClass后是不是还要delete??谢谢回答!!
CMyClass *pClass=(CMyClass *)RUNTIME_CLASS(CMyClass )->CreateObject();方法2:
CMyClass *pClass=new CMyClass ; 这两种方式产生的对象有区别吗??我看RUNTIME_CLASS的源代码,CreateObject()最后调用的也是new 来分配的,那要是这两种没区别,那第一种我产生pClass后是不是还要delete??谢谢回答!!
解决方案 »
- HOOK中不能够修改全局变量的值吗?
- 新手提问,关于位图
- 请高手帮我解决一下对话框的问题,谢谢了,在线等
- 如何动态装载图片
- 04年某校大学三年级网络工程试卷,有谁能搞掂的吗?嘿嘿
- 串口和并口怎么翻译?急!!!谢谢
- 给大家介绍一个网站www.sxsoft.com---软件项目交易网
- Edit控件内能否实现自动完成功能?
- 请帮帮忙!!!
- 在vc6++中怎么调试基本的c++程序cout >>"hello\n";
- Win32 SDK. 在资源创建的对话框上添加一个ListControl 为什么导致对话框无法显示.
- VC 2008里面,“Project”菜单下的“Add Web Reference”项是灰掉的,如何解决?
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)
create好像不要吧!
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;
}
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
}
// 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;//这里
}
IDR_MAINFRAME,
RUNTIME_CLASS(CClientOPCDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CClientOPCView));
AddDocTemplate(pDocTemplate);中的RUNTIME_CLASS(CClientOPCDoc)产生的doc对象,我现在问的是我自己RUNTIME_CLASS出来的一个CRuntimeClass产生的对象。算了,我自己找找吧,明天我加点分,再给你们结贴!!
#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)以为在其它地方赋的值。
文档视图结构中,下面这几句能理解,就说明明白了:
protected: // 仅从序列化创建
CxxxDoc();
DECLARE_DYNCREATE(CxxxDoc)protected: // 仅从序列化创建
CxxxView();
DECLARE_DYNCREATE(CxxxView)