C++核心準(zhǔn)則C.135:使用多重繼承表現(xiàn)多個(gè)不同種類的接口?
和田玉

C.135: Use multiple inheritance to represent multiple distinct interfaces
C.135:使用多重繼承表現(xiàn)多個(gè)不同種類的接口?

Reason(原因)

Not all classes will necessarily support all interfaces, and not all callers will necessarily want to deal with all operations. Especially to break apart monolithic interfaces into "aspects" of behavior supported by a given derived class.
不是所有的類必須支持所有的接口,也不是所有的調(diào)用者都需要所有的操作。最主要的目的是將龐大而僵化的接口分解成被特定派生類支持的行為側(cè)面。

Example(示例)

class?iostream?:?public?istream,?public?ostream?{???//?very?simplified
????//?...
};
istream provides the interface to input operations; ostream provides the interface to output operations. 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接口的結(jié)合,同時(shí)需要在兩個(gè)獨(dú)立的接口之間進(jìn)行同步。

Note(注意)

This is a very common use of inheritance because the need for multiple different interfaces to an implementation is common and such interfaces are often not easily or naturally organized into a single-rooted hierarchy.
由于經(jīng)常會(huì)出現(xiàn)一個(gè)實(shí)現(xiàn)需要多個(gè)不同接口的情況,而且這樣的接口通常不容易或者無(wú)法自然地組織成一個(gè)單根繼承,使用多重繼承成為非常普通的做法。

Note(注意)

Such interfaces are typically abstract classes.
這樣的接口一般都是抽象類。

Enforcement(實(shí)施建議)

???

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c135-use-multiple-inheritance-to-represent-multiple-distinct-interfaces
覺得本文有幫助?請(qǐng)分享給更多人。
關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/p>

