前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.135:使用多重继承表现多个不同种类的接口‍

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

作者头像
面向对象思考
发布2020-03-25 16:42:27
5220
发布2020-03-25 16:42:27
举报
文章被收录于专栏:C++核心准则原文翻译

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

代码语言:javascript
复制
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


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

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

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

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

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

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

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

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