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, 最后两段没太看懂,语法太复杂了。。