何时使用MFC动态对象创建 [英] When to use MFC dynamic object creation
问题描述
我遇到了代码
I came across code
m_pMyView = (CScrollProductionView*)(RUNTIME_CLASS(CScrollProductionView)->CreateObject());
我将代码修改为
I modify the code to
m_pMyView = new CScrollProductionView();
它也可以.我可以知道何时应该使用动态对象创建吗?
0)什么时候应该使用CreateObject(动态对象创建),什么时候应该使用new?
1)CScrollProductionView是从MFC CScrollView继承的类.我应该使用动态对象创建方法吗?我使用传统的新音乐对吗?
2)我意识到当我使用传统的new时,当我使用"delete"时,我将得到运行时错误标量删除析构函数".为什么我知道为什么?
谢谢.
It works too. May I know when should I use dynamic object creation?
0) When should I use CreateObject (dynamic object creation), when I should use new?
1) CScrollProductionView is a class inherited from MFC CScrollView. Should I use dynamic object creation method? Is it right for me to use traditional new?
2) I realize that when I use traditional new, when I use "delete", I will get run-time error "Scalar deleting destructor". Why I know why?
Thanks.
推荐答案
我正在分享我所知道的东西(我不正确)
您是否注意到在创建Document-View应用程序时,我们需要创建一个文档模板并进行注册;使用此文档模板MFC框架创建您的文档视图(即MainFrame,Document,View).
问题是Framework不知道您传递的是哪种类型的类,以便他们可以创建其对象,因此唯一的方法是提供CreateObject函数并在其中编写一个创建例程(即new MyView). >
请展开DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE/RUNTIME_CLASS宏的宏
我想你明白了;-)
谢谢
I am sharing the stuff I know about this(My be no correct)
Have you noticed that while creating a Document-View application, we need to create a document template and register the same; using this document template MFC framework create your document-view ( ie MainFrame, Document, View).
Problem is that Framework doesn''t know what type of class you passed, so that they can create its object, so the only way is to provide a CreateObject function and write a create routine( ie new MyView) inside that.
Please expand the macro of DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE/RUNTIME_CLASS macros
I think you got the things ;-)
Thanks
这篇关于何时使用MFC动态对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!