前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.134:确保所有非常量数据成员具有相同的访问权限‍

C++核心准则C.134:确保所有非常量数据成员具有相同的访问权限‍

作者头像
面向对象思考
发布2020-03-25 16:41:55
7460
发布2020-03-25 16:41:55
举报

C.134: Ensure all non-const data members have the same access level

C.134:确保所有非常量数据成员具有相同的访问权限‍

Reason(原因)

Prevention of logical confusion leading to errors. If the non-const data members don't have the same access level, the type is confused about what it's trying to do. Is it a type that maintains an invariant or simply a collection of values?

避免可能导致错误的逻辑混乱。如果非常量数据成员的访问权限不同,该类型想做什么就模糊不清。这个类型是在维护一个不变量还是简单的数据集合?

Discussion(讨论)

The core question is: What code is responsible for maintaining a meaningful/correct value for that variable?

核心问题是:为于那个变量保持有意义/正确的值是哪部分代码的责任?

There are exactly two kinds of data members:

实际上存在两种类型的数据成员:

  • A: Ones that don't participate in the object's invariant. Any combination of values for these members is valid.
  • A类:没有参与对象不变量的成员。这些成员的值的所有组合都是有效的。
  • B: Ones that do participate in the object's invariant. Not every combination of values is meaningful (else there'd be no invariant). Therefore all code that has write access to these variables must know about the invariant, know the semantics, and know (and actively implement and enforce) the rules for keeping the values correct.
  • B类:参与不变量的成员。不是所有的值组合都有意义(其他的违反不变量)。因此所有需要写访问这些变量的代码必须了解不变量,理解语义,并且知道(并且实际上实现和执行)保持值的正确性的规则。

Data members in category A should just be public (or, more rarely, protected if you only want derived classes to see them). They don't need encapsulation. All code in the system might as well see and manipulate them.

属于分类A的数据成员定义为公有就行(或者,特殊情况下如果你希望派生类能看到它们也会定义为保护)。它们不需要包装。系统中的所有代码可以看到并修改它们。

Data members in category B should be private or const. This is because encapsulation is important. To make them non-private and non-const would mean that the object can't control its own state: An unbounded amount of code beyond the class would need to know about the invariant and participate in maintaining it accurately -- if these data members were public, that would be all calling code that uses the object; if they were protected, it would be all the code in current and future derived classes. This leads to brittle and tightly coupled code that quickly becomes a nightmare to maintain. Any code that inadvertently sets the data members to an invalid or unexpected combination of values would corrupt the object and all subsequent uses of the object.

属于分类B的数据成员应该定义为私有或常量。这是因为封装很重要。将它们定义为非私有和非常量将意味着对象不能控制自己的状态:依靠这个类的无限多的代码在实际维护它的时候需要理解并且遵循不变量;如果它们是保护的,这个范围变成了所有目前和将来的派生类。这会导致脆弱性和紧耦合代码并且很快变成维护的噩梦。任何一段代码不经意地将数据成员修改为无效或非预期的组合都会破坏对象和此后使用这个对象的代码。

Most classes are either all A or all B:

大多数类的成员要么都属于A,要么都属于B:

  • All public: If you're writing an aggregate bundle-of-variables without an invariant across those variables, then all the variables should be public. By convention, declare such classes struct rather than class
  • 都公开:如果你设计聚合一群变量而不包含变量之间的不变量,所有成员都应该定义为公有。按照惯例,将这样的类定义为struct而不是class。
  • All private: If you're writing a type that maintains an invariant, then all the non-const variables should be private -- it should be encapsulated.
  • 都私有:如果设计的类维护一个不变量,那么所有的非常量成员都应该是私有的-它们应该被封装。

Exception(例外)

Occasionally classes will mix A and B, usually for debug reasons. An encapsulated object may contain something like non-const debug instrumentation that isn't part of the invariant and so falls into category A -- it isn't really part of the object's value or meaningful observable state either. In that case, the A parts should be treated as A's (made public, or in rarer cases protected if they should be visible only to derived classes) and the B parts should still be treated like B's (private or const).

偶尔也会存在A,B混合的情况,通常是为方便调试。封装对象可能包含类似非常量调试设施但不是不变量的一部分,因此划归A类-它实际上不是对象值的一部分,也不是有意义的可观测状态。在这种情况下,属于A类的部分应该按照A类成员对待(公开,或者在很特殊的情况下需要只对派生类可见时保护),属于B类的按照B类成员对待(私有或常量)。

Enforcement(实施建议)

Flag any class that has non-const data members with different access levels.

标记那些非常量数据成员具有不同访问权限的类。

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c134-ensure-all-non-const-data-members-have-the-same-access-level


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

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

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档