为什么对象a可以直接访问私有的x(a.x)成员?

那一年的忧伤 2011-05-04 10:04:44
class A{
.....
public:
A(const A&a)
{
x=a.x;
}
private:
int x;
};
为什么对象a可以直接访问私有的x(a.x)成员?
...全文
377 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfk 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 pengzhixi 的回复:]

private; that is, its name can be used only by members and friends of the class in which it is declared.
这句看不懂?
[/Quote]

这么理解吧。
LZ肯定知道的:一个类的成员函数可以访问这个类的私有数据。
而在类A的成员函数中,可以不区分类A的具体对象,也就是pengzhixi所说的:访问控制是针对类而非对象的
ljt3969636 2011-05-04
  • 打赏
  • 举报
回复
你还是当成规定好了。
pengzhixi 2011-05-04
  • 打赏
  • 举报
回复
private; that is, its name can be used only by members and friends of the class in which it is declared.
这句看不懂?
wwyyxx26 2011-05-04
  • 打赏
  • 举报
回复
c++就是这样规定的
那一年的忧伤 2011-05-04
  • 打赏
  • 举报
回复
楼上的,咱是初学者,还是不太懂,可否再详细说一下哈。。。- -
ljt3969636 2011-05-04
  • 打赏
  • 举报
回复
因为A(const A&a)是他的成员函数。或许这么回答你能帮助你:访问限制标号是针对类而不是针对一个类的不同对象,只要同属一个类就可以不用区分同一个类得不同对象。
pengzhixi 2011-05-04
  • 打赏
  • 举报
回复
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.


帖标准。加一句,访问控制是针对类而非对象的。

64,659

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧