C ++类/ struct成员的默认可见性 [英] default visibility of C++ class/struct members

查看:290
本文介绍了C ++类/ struct成员的默认可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,为什么private是类成员的默认可见性,而对于结构体是public的?

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

推荐答案

作为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.

这篇关于C ++类/ struct成员的默认可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆