C++ 类/结构成员的默认可见性

2021-12-23 00:00:00 class struct c++ member

在 C++ 中,为什么类成员的默认可见性是私有的,而结构是公共的?

In C++, why is private the default visibility for members of classes, but public for structs?

推荐答案

C++ 是作为 C 的超集引入的.结构是从 C 继承而来的,其成员的语义是公共的.存在大量 C 代码,包括希望与 C++ 一起使用的库,它们使用结构.类是在 C++ 中引入的,为了符合 OO 的封装哲学,它们的成员默认是私有的.

C++ was introduced as a superset of C. Structs were carried over from C, where the semantics of their members was that of public. A whole lot of C code exists, including libraries that were desired to work with C++ as well, that use structs. Classes were introduced in C++, and to conform with the OO philosophy of encapsulation, their members are private by default.

相关文章