C++核心准则C.162:将大致相同的操作设计为重载函数

C.162: Overload operations that are roughly equivalent

C.162:将大致相同的操作设计为重载函数

 

Reason(原因)

Having different names for logically equivalent operations on different argument types is confusing, leads to encoding type information in function names, and inhibits generic programming.

逻辑上等价,只是参数不同的操作具有不同的名称是难以理解的,会导致函数名中包含类型信息,而且阻碍编程共通化。

Example(示例)

Consider(考虑下面的代码):

void print(int a);
void print(int a, int base);
void print(const string&);

These three functions all print their arguments (appropriately). Conversely:

这三个函数都(恰当地)打印参数指定的内容。相反地,

void print_int(int a);
void print_based(int a, int base);
void print_string(const string&);

These three functions all print their arguments (appropriately). Adding to the name just introduced verbosity and inhibits generic code.

这三个函数都(恰当地)打印参数指定的内容。只是在函数名中增加了冗长的,阻碍共通化编程的信息。

Enforcement(实施建议)

???

原文链接:

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


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

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

原创文章 414 获赞 724 访问量 35万+

猜你喜欢

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