什么时候使用C ++私人继承的组合? [英] When to use C++ private inheritance over composition?

查看:284
本文介绍了什么时候使用C ++私人继承的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能给我一个具体的例子什么时候更喜欢使用私人继承而不是组合?就个人而言,我将使用组合私人继承,但可能有一种情况,使用私人继承是一个特定问题的最佳解决方案。阅读 C ++常见问题,为您提供了使用私有继承的示例,但我

Can you give me a concrete example when is preferable to use private inheritance over composition? Personally, I will use composition over private inheritance, but there might be the case that using private inheritance is the best solution for a particular problem. Reading the C++ faq, gives you an example on using private inheritance, but I seems easier to use composition + strategy pattern or even public inheritance than private inheritance.

推荐答案

private

private inheritance is typically used to represent "implemented-in-terms-of". The main use I have seen is for mixins using private multiple inheritance to build up a child object with the proper functionality from the various mixin parents. This can also be done with composition (which I slightly prefer) but the inheritance method DOES allow you to use using to expose some parent methods publicly, and allows for a slightly more convenient notation when using the mixin methods.

这篇关于什么时候使用C ++私人继承的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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