zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 22: Declare data members private.

    If everything in the public interface is a function, clients won't have to scratch their heads trying to remember whether to use parentheses when they want to access a member of the class. They'll just do it, because everything is a function.

    Using functions gives you much more precise control over the accessibility of data members.

    If you implement access to a data member through a function, you can later replace the data member with a computation, and nobody using your class will be any the wiser.

    Hiding data members behind functional interfaces can offer all kinds of implementation flexibility. For example, it makes it easy to notify other objects when data members are read or written, to verify class invariants and function pre- and postconditions, to perform synchronization in threaded environments, etc.

    The argument against protected data members is similar.

    Suppose we have a public data member, and we eliminate it. How much code might be broken? All the client code that uses it, which is generally an unknowably large amount. Public data members are thus completely unencapsulated. But suppose we have a protected data member, and we eliminate it. How much code might be broken now? All the derived classes that use it, which is, again, typically an unknowably large amount of code. Protected data members are thus as unencapsulated as public ones, because in both cases, if the data members are changed, an unknowably large amount of client code is broken. This is unintuitive, but as experienced library implementers will tell you, it’s still true. Once you’ve declared a data member public or protected and clients have started using it, it’s very hard to change anything about that data member. Too much code has to be rewritten,retested, redocumented, or recompiled. From an encapsulation point of view, there are really only two access levels: private (which offers encapsulation) and everything else (which doesn't).

    Things to Remember:

    • Declare data members private. It gives clients syntactically uniform access to data, affords fine-grained access control, allows invariants to be enforced, and offers class authors implementation flexibility.
    • protected is no more encapsulated than public.
  • 相关阅读:
    【LCT维护基环内向树森林】BZOJ4764 弹飞大爷
    【LCT】BZOJ3091 城市旅行
    【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版
    【最大权闭合子图】bzoj4873 [Shoi2017]寿司餐厅
    【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
    【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)
    【费用流】BZOJ1061: [Noi2008]志愿者招募(这题超好)
    从输入url到页面加载的过程
    forEach和map的区别
    理解 JavaScript 对象原型、原型链如何工作、如何向 prototype 属性添加新的方法。
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15239115.html
Copyright © 2011-2022 走看看