前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.136:使用多重继承表现“实现属性”的组合

C++核心准则C.136:使用多重继承表现“实现属性”的组合

作者头像
面向对象思考
发布2020-03-25 16:43:16
4200
发布2020-03-25 16:43:16
举报

C.136: Use multiple inheritance to represent the union of implementation attributes

C.136:使用多重继承表现“实现属性”的组合。‍

Reason(原因)

Some forms of mixins have state and often operations on that state. If the operations are virtual the use of inheritance is necessary, if not using inheritance can avoid boilerplate and forwarding.

某些形式的混入通常包含状态和针对状态的操作。如果操作是虚的,使用继承就是必要的,如果不使用继承可以避免样板和转交。

mixins就是定义一部分公共的方法或属性,然后混入到各个组件中使用,这样可以方便管理与修改

Example(示例)

代码语言:javascript
复制
class iostream : public istream, public ostream {   // very simplified
    // ...
};

istream provides the interface to input operations (and some data); ostream provides the interface to output operations (and some data). iostream provides the union of the istream and ostream interfaces and the synchronization needed to allow both on a single stream.

istream提供输入操作的接口(和一些数据);ostream提供了输出操作的接口(和一些数据)。iostream提供istream和ostream接口的组合,同时每个单独的流上都需要允许同步。

Note(注意)

his a relatively rare use because implementation can often be organized into a single-rooted hierarchy.

这是一种相对稀少的用法,因为实现通常可以组织成一个单根继承中。

Example(示例)

Sometimes, an "implementation attribute" is more like a "mixin" that determine the behavior of an implementation and inject members to enable the implementation of the policies it requires. For example, see std::enable_shared_from_this or various bases from boost.intrusive (e.g. list_base_hook or intrusive_ref_counter).

有时,一个“实现属性"更像一个"minxin",这个"minxin”可以决定一个实现的行为,也可以是使能实现它要求的原则的注入成员。例如,参考std::enable_shared_from_this或者来自boost.intrusive的很多基础类(例如list_base_hook或者instrusive_ref_counter)。

Boost.Instrusive(

https://theboostcpplibraries.com/boost.intrusive

)特别适合高性能编程的C++库。

Enforcement(实施建议)

???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c136-use-multiple-inheritance-to-represent-the-union-of-implementation-attributes


觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档