前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.4:只有直接访问表达的函数,才应该成为成员

C++核心准则C.4:只有直接访问表达的函数,才应该成为成员

作者头像
面向对象思考
发布2020-03-25 15:52:25
3290
发布2020-03-25 15:52:25
举报

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(示例)

代码语言:javascript
复制
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:

一组重载函数中也许会有某个成员不会直接访问私有数据。

代码语言:javascript
复制
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:

类似地,一组函数可能被设计用来串联使用。

代码语言:javascript
复制
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

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

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

有任何疑问,欢迎留言提问或讨论。


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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.4: Make a function a member only if it needs direct access to the representation of a class
    • Reason(原因)
      • Example(示例)
        • Note(注意)
          • Exception(例外)
            • Exception(类外)
              • Exception(列外)
                • Exception(例外)
                  • Enforcement(实施建议)
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档