为什么数据成员在C ++中默认是私有的? [英] Why data members are private by default in C++?

查看:192
本文介绍了为什么数据成员在C ++中默认是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,默认情况下,将类中的所有数据成员设置为私有是什么特殊原因?

Is there any particular reason for having all data members in a class as private by default in C++?

谢谢,
Naga

Thanks, Naga

推荐答案

因为它更好地被正确封装,只打开需要的东西,而不是一切打开默认情况下,并关闭它。

Because it's better to be properly encapsulated and only open up the things that are needed, as opposed to having everything open by default and having to close it.

封装(信息隐藏)是一种很好的东西,像安全性(例如,锁定网络服务)一样,默认应该是好的而不是坏的。

Encapsulation (information hiding) is a good thing and, like security (for example, the locking down of network services), the default should be towards good rather than bad.

这篇关于为什么数据成员在C ++中默认是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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