default visibility of C++ class/struct members

前端 未结 3 1910

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

3条回答
  •  悲&欢浪女
    2020-12-02 12:39

    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.

提交回复
热议问题