前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C.163: 重载只用于基本等价的操作

C.163: 重载只用于基本等价的操作

作者头像
面向对象思考
发布2020-03-25 16:58:26
2660
发布2020-03-25 16:58:26
举报

C.163: Overload only for operations that are roughly equivalent

C.163: 重载只用于基本等价的操作

Reason(原因)

Having the same name for logically different functions is confusing and leads to errors when using generic programming.

逻辑上不同的函数使用相同的名称会引起混淆,在使用它们进行共通化编程时容易引发错误。

Example(示例)

Consider(考虑下面的代码):

代码语言:javascript
复制
void open_gate(Gate& g);   // remove obstacle from garage exit lane
void fopen(const char* name, const char* mode);   // open file

The two operations are fundamentally different (and unrelated) so it is good that their names differ. Conversely:

两个操作是根本不同的(也没有关联),因此它们使用不同的名称就很好。相反:

代码语言:javascript
复制
void open(Gate& g);   // remove obstacle from garage exit lane
void open(const char* name, const char* mode ="r");   // open file

The two operations are still fundamentally different (and unrelated) but the names have been reduced to their (common) minimum, opening opportunities for confusion. Fortunately, the type system will catch many such mistakes.

这还是两个根本不同的操作(而且毫无关联),但是名称已经被压缩到(共通的)最小限度,这种做法增加了混淆的可能性。幸运的是,很多这样的错误都会被系统捕捉到。

Note(注意)

Be particularly careful about common and popular names, such as open, move, +, and ==.

特别关注那些通用和常见的名字,例如open,move,+和==等。

Enforcement(实施建议)

???

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c163-overload-only-for-operations-that-are-roughly-equivalent


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

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

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.163: Overload only for operations that are roughly equivalent
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档