C++核心準則C.136:使用多重繼承表現“實現屬性”的組合

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

 


 

覺得本文有幫助?歡迎點贊並分享給更多的人。

閱讀更多更新文章,請關注微信公衆號【面向對象思考】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章