default visibility of C++ class/struct members

前端 未结 3 1911

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

3条回答
  •  醉酒成梦
    2020-12-02 12:44

    Because a class is a usual way of doing object orientation, which means that member variables should be private and have public accessors - this is good for creating low coupling. Structs, on the other hand, have to be compatible with C structs, which are always public (there is no notion of public and private in C), and don't use accessors/mutators.

提交回复
热议问题