zoukankan      html  css  js  c++  java
  • 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, 最后两段没太看懂,语法太复杂了。。



  • 相关阅读:
    2018.7.26笔记(变量的数据类型,if语句)
    id(),is 和 ==的区别,编码和解
    2018.7.31笔记(列表的基本操作)
    阅读与感悟如何高效学习
    说说设计模式 单例模式
    简单说说Java知识点 多线程
    阅读与感悟联盟
    阅读与感悟非暴力沟通
    简单说说Java知识点 HashMap
    MySQL知识树存储引擎
  • 原文地址:https://www.cnblogs.com/mosakashaka/p/12534682.html
Copyright © 2011-2022 走看看