Why are data members private by default in C++?

后端 未结 5 1204
挽巷
挽巷 2021-02-06 01:23

Is there any particular reason that all data members in a class are private by default in C++?

5条回答
  •  -上瘾入骨i
    2021-02-06 01:59

    The Design and Evolution of C++

    2.10 The Protection Model

    Before starting work on C with Classes, I worked with operating systems. The notions of protection from the Cambridge CAP computer and similar systems - rather than any work in programming languages - inspired the C++ protection mechanisms. The class is the unit of protection and the fundamental rule is that you cannot grant yourself access to a class; only the declarations placed in the class declaration (supposedly by its owner) can grant access. By default, all information is private.

提交回复
热议问题