const定义

来自http://duramecho.com/ComputerInformation/WhyHowCppConst.html,节选了一下


定义中添加const,按目前的理解,应该说是为了更快的发现代码中的问题(编译时就会发现对const变量的修改),似乎没有非如此定义不可的必要,不过必要这个词又应该是如何定义的。。


比较重要的一句话:

Basically ‘const’ applies to whatever is on its immediate left (other than if there is nothing there in which case it applies to whatever is its immediate right).

const优先向左结合的,所以类似

<code> int * const Constant3;<code />

这样的定义,那么这个指针的值不能改变,而指向的数据可以改变。


这里想到了关于变量的定义,找一个变量的基类型优先右结合,比如

int *a(void){}

那么a是一个函数而非函数指针,或许说得不严谨吧。。


打个岔,文章里看到一句话正是我想说的

The addition of an ‘&’ to the parameter name in C++ (which was a very confusing choice of symbol because an ‘&’ in front of variables elsewhere in C generates pointers!) causes the actual variable itself, rather than a copy, to be used as the parameter in the subroutine and therefore can be written to thereby passing data back out the subroutine.


类中:

class Class1
{ void Method1();
  int MemberVariable1;}

Of course one sometimes needs to combine some of these different uses of ‘const’ which can get confusing as in

const int*const Method3(const int*const&)const;

where the 5 uses ‘const’ respectively mean that the variable pointed to by the returned pointer & the returned pointer itself won’t be alterable and that the method does not alter the variable pointed to by the given pointer, the given pointer itself & the object of which it is a method!.


That's all, 最后两段没太看懂,语法太复杂了。。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值