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:
实际上存在两种类型的数据成员:
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:
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
觉得本文有帮助?请分享给更多人。
关注【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有