C++核心准则C.135:使用多重继承表现多个不同种类的接口

C.135: Use multiple inheritance to represent multiple distinct interfaces

C.135:使用多重继承表现多个不同种类的接口‍

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.

不是所有的类必须支持所有的接口,也不是所有的调用者都需要所有的操作。最主要的目的是将庞大而僵化的接口分解成被特定派生类支持的行为侧面。

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接口的结合,同时需要在两个独立的接口之间进行同步。

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.

由于经常会出现一个实现需要多个不同接口的情况,而且这样的接口通常不容易或者无法自然地组织成一个单根继承,使用多重继承成为非常普通的做法。

Note(注意)

Such interfaces are typically abstract classes.

这样的接口一般都是抽象类。

Enforcement(实施建议)

???

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c135-use-multiple-inheritance-to-represent-multiple-distinct-interfaces


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

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

发布了410 篇原创文章 · 获赞 677 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/craftsman1970/article/details/105315765