大家好,欢迎来到IT知识分享网。
C.4: Make a function a member only if it needs direct access to the representation of a class
C.4:只有直接访问表达的函数,才允许成为成员。
Reason(原因)
Less coupling than with member functions, fewer functions that can cause trouble by modifying object state, reduces the number of functions that needs to be modified after a change in representation.
和使用成员函数相比耦合性略低,较少函数可以通过修改对象状态带来麻烦,减少改变类表达时需要修改的函数的数量。
Example(示例)
class Date { // ... relatively small interface ... }; // helper functions: Date next_weekday(Date); bool operator==(Date, Date);
The “helper functions” have no need for direct access to the representation of a Date.
“帮助函数”没有需求要直接访问Data的表达。
Note(注意)
This rule becomes even better if C++ gets “uniform function call”.
如果C++可以导入“统一函数调用”这条准则甚至会变得更完美。
译者注:“uniform funcation call”是C++之父本人提出的C++语法建议。核心是对于一个形如f(x,y)的函数调用,如果不存在函数f(x,y),可以转而调用x.f(y)。有了这个语法,编写非成员帮助函数的灵活性将会进一步加大。
Exception(例外)
The language requires virtual functions to be members, and not all virtual functions directly access data. In particular, members of an abstract class rarely do.
(C++)语言要求虚函数必须是成员,而且不是所有的虚函数都会直接访问数据。通常抽象类的成员很少直接访问数据。
Note multi-methods.
注意:多方法(链接:https://parasol.tamu.edu/~yuriys/papers/OMM10.pdf)
Exception(类外)
The language requires operators =, (), [], and -> to be members.
语言要求=,(),[]和->运算符作为成员存在。
Exception(列外)
An overload set may have some members that do not directly access private data:
一组重载函数中也许会有某个成员不会直接访问私有数据。
class Foobar { public: void foo(long x) { /* manipulate private data */ } void foo(double x) { foo(std::lround(x)); } // ... private: // ... };
Exception(例外)
Similarly, a set of functions may be designed to be used in a chain:
类似地,一组函数可能被设计用来串联使用。
x.scale(0.5).rotate(45).set_color(Color::red);
Typically, some but not all of such functions directly access private data.
通常,有些但不是所有这样的函数都会直接访问私有数据
Enforcement(实施建议)
- Look for non-virtual member functions that do not touch data members directly. The snag is that many member functions that do not need to touch data members directly do.寻找没有直接接触数据成员的非虚成员函数。讽刺的是存在许多不需要直 接访问数据成员的成员函数。
- Ignore virtual functions.忽略虚函数。
- Ignore functions that are part of an overload set out of which at least one function accesses private members.如果一组重载函数中至少有一个函数访问了私有成员,那么忽略其他函数。
- Ignore functions returning this.忽略返回this指针的函数。
英文原文地址:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c3-represent-the-distinction-between-an-interface-and-an-implementation-using-a-class
觉得本文有帮助?请分享给更多人。
更多更新文章,欢迎关注微信公众号【面向对象思考】
面向对象设计,面向对象编程,面向对象思考!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/56375.html