C.4:只有直接访问表达的函数,才应该成为成员。
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.
和使用成员函数相比普通函数耦合性略低,一方面可以通过修改对象状态带来麻烦的函数会变少,另一方面可以减少改变类表达时需要修改的函数的数量。
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的表达。
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)。有了这个语法,编写非成员帮助函数的灵活性将会进一步加大。
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)
The language requires operators =
, ()
, []
, and ->
to be members.
语言要求=,(),[]和->运算符作为成员存在。
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:
// ...
};
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.
通常,有些但不是所有这样的函数都会直接访问私有数据
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.
寻找没有直接接触数据成员的非虚成员函数。讽刺的是存在许多不需要直 接访问数据成员的成员函数。
virtual
functions.
忽略虚函数。
private
members.如果一组重载函数中至少有一个函数访问了私有成员,那么忽略其他函数。
this
.
忽略返回this指针的函数。
英文原文地址:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c3-represent-the-distinction-between-an-interface-and-an-implementation-using-a-class
觉得本文有帮助?请分享给更多人。
关注【面向对象思考】轻松学习每一天!
有任何疑问,欢迎留言提问或讨论。
面向对象开发,面向对象思考!