前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.9:最小限度暴露成员

C++核心准则C.9:最小限度暴露成员

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

C.9: Minimize exposure of members

C.9:最小限度暴露成员

Reason(原因)

Encapsulation. Information hiding. Minimize the chance of unintended access. This simplifies maintenance.

封装性。信息隐藏。将非故意访问的可能性降至最低。简化维护。

译者注:这是封装最直接的好处。

Example(示例)

代码语言:javascript
复制
template<typename T, typename U>
struct pair {
    T a;
    U b;
    // ...
};

Whatever we do in the //-part, an arbitrary user of a pair can arbitrarily and independently change its a and b. In a large code base, we cannot easily find which code does what to the members of pair. This may be exactly what we want, but if we want to enforce a relation among members, we need to make them privateand enforce that relation (invariant) through constructors and member functions. For example:

无论我们在注释的位置做什么,pair的任何用户都可以随意、独立的修改数据成员a和b。在大规模代码中,我们无法简单地发现是哪段代码对pair的成员做了什么。如果这就是我们想要的当然没问题,但是如果我们想要加强成员间联系,就需要将它们变更为私有成员并通过构造函数和成员函数强化这种联系(不变式)。例如:

代码语言:javascript
复制
class Distance {
public:
    // ...
    double meters() const { return magnitude*unit; }
    void set_unit(double u)
    {
            // ... check that u is a factor of 10 ...
            // ... change magnitude appropriately ...
            unit = u;
    }
    // ...
private:
    double magnitude;
    double unit;    // 1 is meters, 1000 is kilometers, 0.001 is millimeters, etc.
};
Note(注意)

If the set of direct users of a set of variables cannot be easily determined, the type or usage of that set cannot be (easily) changed/improved. For public and protected data, that's usually the case.

如果无法简单地决定一组变量的直接用户,这组变量的类型和用法就无法(简单地)变更和改善。公有或保护成员,基本上属于这种情况。

Example(示例)

A class can provide two interfaces to its users. One for derived classes (protected) and one for general users (public). For example, a derived class might be allowed to skip a run-time check because it has already guaranteed correctness:

一个类可以向用户提供两类接口。一类面向派生类(保护成员),一个面向普通用户(公有成员)。例如,由于已经保证了正确性,某个派生类可能会允许省略某项运行时检查。

代码语言:javascript
复制
class Foo {
public:
    int bar(int x) { check(x); return do_bar(x); }
    // ...
protected:
    int do_bar(int x); // do some operation on the data
    // ...
private:
    // ... data ...
};

class Dir : public Foo {
    //...
    int mem(int x, int y)
{
        /* ... do something ... */
        return do_bar(x + y); // OK: derived class can bypass check
    }
};

void user(Foo& x)
{
    int r1 = x.bar(1);      // OK, will check
    int r2 = x.do_bar(2);   // error: would bypass check
    // ...
}
Note(注意)

protected data is a bad idea.

保护型数据是个坏主意。

Note(注意)

Prefer the order public members before protected members before private members see.

较好的顺序是公有成员在保护成员之前,保护成员又在私有成员之前。

译者注:还有一个类似的规则是:如果成员之间有调用关系,那么调用者在前,被调用者在后。

Enforcement(实施建议)
  • Flag protected data. 在发现保护型数据时进行提示。
  • Flag mixtures of public and private data

在发现公有和私有数据混杂在一起时进行提示。

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c9-minimize-exposure-of-members

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

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

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


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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.9: Minimize exposure of members
    • Reason(原因)
      • Example(示例)
        • Note(注意)
          • Example(示例)
            • Note(注意)
              • Note(注意)
                • Enforcement(实施建议)
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档