如何不使用虚拟继承?
我正在制作一个win32 api程序。我首先创建了一个名为 WinClass 的基类,并像其他十几个类一样继承了它。现在我需要从继承自基类 WinControl 和 WinHandler 的两个类创建一个派生类。因为我打算从最初的十几个派生类中创建更多的派生类,所以我'就像从 WinClass 继承的每个类一样,必须使用虚拟继承。那么有没有办法不使用虚拟继承来做到这一点?
I am making a win32 api program. I first created a base class called WinClass and inherited like a dozen other classes from it. Now I need to create a derived class from two classes inherited from base class WinControl and WinHandler.Since I intend to make many more derived classes out of the original dozen, I'll have to use virtual inheritance on like every class inherited from WinClass.So is there any way to do this without using virtual inheritance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
了解如何完全避免过度使用继承。例如,阅读这篇文章
http://berniespiration.com/ software/inheritance-is-evil-and-must-be-destroyed/
学习如何按照您想要的方式完成工作(正确使用继承)的一个好的开始是“设计”一书模式”:
http://c2.com/cgi/wiki?DesignPatternsBook
Learn how to avoid overuse of inheritance at all. For example, read this article
http://berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
A good start to learn how to get things done the way you want it (with the correct use of inheritance) is the book "Design Patterns":
http://c2.com/cgi/wiki?DesignPatternsBook
这是你的第一个问题。如果说 WPF 教会了我们什么的话,那就应该是继承不一定是 GUI 设计的最佳模型。
是的:不要从
WinClass
派生WinControl
和WinHandler
。你还没有说这些的作用,所以我无法提供任何具体的建议。确实,看起来您需要的是“有一个”关系,而不是继承带来的“是一个”关系。另外,类名不需要使用粗体。只需使用 StackOverflow 提供的代码标签即可。也没有必要用粗体SHOUT。
And there's your first problem. If WPF has taught us anything, it should be that inheritance is not necessarily the best model for GUI design.
Yes: don't have
WinControl
andWinHandler
be derived fromWinClass
. You haven't said what these do, so I can't offer any specific advice about them. Really, it seems like what you need is a "has a" relationship rather than the "is a" relationship that inheritance brings.Also, there's no need to use boldface for class names. Just use the code tags that StackOverflow provides. Nor is there a need to SHOUT in bold-face.