MFC派生我自己的班级 [英] MFC Deriving my own class

查看:61
本文介绍了MFC派生我自己的班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在MFC中创建自己的UI组件。我从 CWnd 类派生它,我将覆盖 OnPaint()方法。我的问题涉及方法 Create(),它继承自 CWnd 。我想知道从构造函数调用它是否合适(也许不是,因为创建时不会调用虚方法)或者我将让类的客户端调用 Create()他自己的方法(我不喜欢)...



感谢您的意见。



祝你好运,

JK

Hi,

I am creating my own UI component in MFC. I derived it from CWnd class and I am going to override OnPaint() method. My question concerns the method Create(), that is inherited from CWnd . I would like to know if it is good to call that from the constructor (perhaps not, because virtual methods shall not be called while creation) or I shall let the client of the class to call the Create() method on his own (which I dont like)...

Thanks for your opinions.

Best regards,
J.K.

推荐答案

我担心你应该使用Create方法:一般窗口创建序列 [ ^ ]



最好的问候

Espen Harlinn
I''m afraid you should use the Create approach: General Window Creation Sequence[^]

Best regards
Espen Harlinn


MFC的理念是一个两步创建过程,所以分离构造函数和创建调用。为了向其他MFC类提供一致的行为,我会坚持使用它。



调用者调用Create的第二个好理由是来自构造函数的你报告任何错误的手段有限。您可以抛出异常,而在单独的Create函数中,您可以返回一个值。
MFC''s philosophy is a 2-step creation process, so a separation of constructor and create call. I would just stick with that for reasons of presenting a consistent behavior to other MFC classes.

A second good reason for having the caller call Create is that from a constructor you have limited means of reporting any errors. You can just throw an exception, whereas in a separate Create function you can return a value.


这篇关于MFC派生我自己的班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆