C++核心准则C.136:使用多重继承表现“实现属性”的组合
发布日期:2021-07-01 05:27:04 浏览次数:2 分类:技术文章

本文共 1848 字,大约阅读时间需要 6 分钟。

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(示例)

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

 


 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

转载地址:https://oothinking.blog.csdn.net/article/details/105331639 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:c++核心准则C.137: 使用虚基类避免过于一般的基类
下一篇:C++核心准则C.135:使用多重继承表现多个不同种类的接口

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年05月03日 11时06分00秒