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



  • 相关阅读:
    例题6-8 Tree Uva548
    例题6-7 Trees on the level ,Uva122
    caffe Mac 安装
    Codeforces Round #467 (Div. 1) B. Sleepy Game
    Educational Codeforces Round37 E
    Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
    Good Bye 2017 E. New Year and Entity Enumeration
    Good Bye 2017 D. New Year and Arbitrary Arrangement
    Codeforces Round #454 D. Seating of Students
    浙大紫金港两日游
  • 原文地址:https://www.cnblogs.com/mosakashaka/p/12534682.html
Copyright © 2011-2022 走看看