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​