免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: aaaaal
打印 上一主题 下一主题

私有成员可以这样访问? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-07-28 22:58 |只看该作者

回复 #10 aaaaa5aa 的帖子

拷贝构造函数为何可以调用引用参数对象的私有成员?

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
12 [报告]
发表于 2009-07-28 23:43 |只看该作者

回复 #11 aaaaal 的帖子

我还是好心把以前的帖子搜出来吧……
http://bbs.chinaunix.net/viewthread.php?tid=1473750#pid10665029



以下是对以前发帖的补充。

C++标准有一整章(11 Member access control)是讲这个的。
下面是一部分:
A member of a class can be
— private; that is, its name can be used only by members and friends of the class in which it is
declared.
— protected; that is, its name can be used only by members and friends of the class in which it is
declared, and by members and friends of classes derived from this class (see 11.5).
— public; that is, its name can be used anywhere without access restriction.


c_string_ 是MyString的一个private成员。它的名字只能被MyString的成员和友元使用。
复制构造函数是MyString的一个“成员(函数)”, 该成员函数能使用c_string_这个名字并没有什么不合理之处。



《The C++ Programming Language》中的描述似乎更好一些:
15.3 Access Control [hier.access]
A member of a class can be p r i v a t e , p r o t e c t e d , or p u b l i c :
– If it is p r i v a t e , its name can be used only by member functions and friends of the class in
which it is declared.
– If it is p r o t e c t e d , its name can be used only by member functions and friends of the class in
which it is declared and by member functions and friends of classes derived from this class
(see §11.5).
– If it is p u b l i c , its name can be used by any function.


更加强调了, 访问级限制的是函数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP