关于c++常量函数(const member function)

在c++ primer中对于常最函数的解析如下:
  • By default, the type of this is a const pointer to nonconst version of the class type. However, in the case, we cannot bind this to a const object. And means we cannot call an ordinary member function on a const object.
  • There is no place to indicate that this should be a pointer to const. The language resolves this problem by letting this us put const after the parameter list of a member function. A const following the parameter list indicates that this is a pointer to const.
解析
  • 在默认情况下,this是一个指向非const类型的const指针,那么,在这种情况下this不能绑定在一个const对象上,也就意味着我们不能调用const对象的常规成员函数。Note: this指针的绑定是由编译器指执行的,可以理解为Class_Name::member(&object),同时this = &object,此时this是Class_Name *const
  • 由于this是由编译器绑定的,我们不能直接定义this的类型,cpp中使用常量函数这种形式来指示在此函数中this为一个指向const类型的const指针,这同时也可以解释在常量函数中为什么不能修改成量变量的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值