【翁恺】10-访问限制

setting limits

  • to keep the client programmer's hands off members they shouldn't touch
  • to allow the library designer to change the internal working of the structure without worrying about how it will affect the client programmer

C++ access control

  • the members of a class can be cataloged,marked as:
    • public
    • private
    • protected

public公开的。所有的都可以访问

private私有的。类的成员函数可以访问这些私有的成员函数或者成员变量; private是对类来说的不是对对象来说的,同一个类的对象是可以互相访问私有变量的,private的这种限制仅仅是在编译时刻,运行时刻仍然有办法访问别的类的私有成员

protected。类自己以及子子孙孙可以访问

public

  • public means all member declarations that follow are available to everyone

private

  • the private keyword means that no one can access that member except inside function members of that type

自己指的是这个类的成员函数。

private是对类来说的,不是对对象的。同一个类的对象之间是可以互相访问私有的成员变量的。private的限制仅仅在编译时刻,意味着,运行时刻可以访问私有的东西。C++的OOP 仅在源代码时候存在,编译后的 .o 和其他语言是一样的,不会在运行时刻检查访问限制。

friends

  • to explicitly grant access to a function that isn't member of the structure
  • the class itself controls which code has access to its members
  • can declare a global function as a friend, as well as a member function of another class,or even an entire class,as a friend

别的类,别的函数,别的类内的某个函数

friend的授权也是在编译时刻检查的。运算符重载时候用的较多。

class vs. struct

  • class defaults to private
  • struct defaults to public

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值