开发者

How NOT to use virtual inheritance?

开发者 https://www.devze.com 2023-03-19 20:10 出处:网络
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 bas

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开发者_运维百科?


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


I first created a base class called WinClass and inherited like a dozen other classes from it.

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.

So is there any way to do this WITHOUT using virtual inheritance?

Yes: don't have WinControl and WinHandler be derived from WinClass. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号