<pre class="cjk" align="LEFT" name="code" style="background-color: rgb(255, 255, 255); margin-bottom: 0cm; white-space: pre-wrap; word-wrap: break-word; line-height: 0.47cm; widows: 2; orphans: 2;"><strong><span style="font-size:18px;"><span style="font-family: FangSong_GB2312;"></span></span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">在普通的非 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数中,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的类型是一个指向类类型的 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">指针</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。可以改变</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">所指向的值,但不能改变 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">所保存的地址。 在 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数中,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的类型是一个指向 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">类类型对象的 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">指针。既不能改变 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">所指向的对象,也不能改变 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">所保存的地址。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">看到</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">关键字,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">C++</span><span style="font-family: FangSong_GB2312; font-size: 12px;">程序员首先想到的可能是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">常量。这可不是良好的条件反射。如果只知道用</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">定义常量,那么相当于把火药仅用于制作鞭炮。</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">更大的魅力是它可以修饰函数的参数、返回值,甚至函数的定义体。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">const </span><span style="font-family: FangSong_GB2312; font-size: 12px;">是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">constant</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的缩写,“恒定不变”的意思。被</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。所以很多</span><span style="font-family: FangSong_GB2312; font-size: 12px;">C++</span><span style="font-family: FangSong_GB2312; font-size: 12px;">程序设计书籍建议:“</span><span style="font-family: FangSong_GB2312; font-size: 12px;">Useconst whenever you need”</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span></strong><span style="background-color: rgb(240, 240, 240); font-family: FangSong_GB2312; font-size: 12px; font-weight: bold; line-height: 0.47cm; text-indent: 0.74cm;"> </span>
<span style="font-family:FangSong_GB2312;"><strong style="background-color: rgb(255, 255, 0);">预备知识:</strong></span>
<span style="font-family: FangSong_GB2312; font-size: 12px; font-weight: bold; background-color: rgb(240, 240, 240); line-height: 0.47cm; text-indent: 0.74cm;">如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况: </span><strong><span style="font-family: FangSong_GB2312; font-size: 12px;"><a target=_blank name="0" style="color: rgb(34, 0, 0); font-family: Arial; font-size: 14px; line-height: 26px;"></a></span></strong><div class="hdwiki_nrwenzih1"> int b = 500; const int* a = &b; [1] int const *a = &b; [2] int* const a = &b; [3] const int* const a = &b; [4] 如果你能区分出上述四种情况,那么,恭喜你,你已经迈出了可喜的一步。不知道,也没关系,我们可以参考《effective c++》item21上的做法,如果const位于星号的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;如果const位于星号的 右侧,const就是修饰指针本身,即指针本身是常量。因此,[1]和[2]的情况相同,都是指针所指向的内容为常量,这种情况下不允许对内容进行更改操 作,如不能*a = 3 ;[3]为指针本身是常量,而指针所指向的内容不是常量,这种情况下不能对指针本身进行更改操作,如a++是错误的;[4]为指针本身和指向的内容均为常 量。 </div> 1.<span style="font-family: FangSong_GB2312; font-size: 12px;"><span style="background-color: rgb(255, 255, 0);">用const</span><span style="background-color: rgb(255, 255, 0);">修饰函数的参数 </span></span>
<span style="font-weight: bold;"><span style="font-family: FangSong_GB2312; font-size: 12px;">如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰,否则该参数将失去输出功能。<span style="font-family: 'AR PL UMing HK', serif;"><span style="background-color: rgb(0, 255, 255);">const</span></span><span style="background-color: rgb(0, 255, 255);">只能修饰输入参数:</span></span></span><span style="font-family: FangSong_GB2312; font-size: 12px; font-weight: bold; background-color: rgb(240, 240, 240); line-height: 0.47cm; text-indent: 0.74cm;">(函数可以通过形参输出结果,即由输出参数“带回”结果)</span>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"><u>如果输入参数采用“指针传递”,那么加const修饰可以防止意外地改动该指针所指向的内容,起到保护作用。</u></span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">例如</span><span style="font-family: FangSong_GB2312; font-size: 12px;">StringCopy</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数:</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">void StringCopy(char*strDestination, const char *strSource);</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">其中</span><span style="font-family: FangSong_GB2312; font-size: 12px;">strSource</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是输入参数,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">strDestination</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是输出参数。给</span><span style="font-family: FangSong_GB2312; font-size: 12px;">strSource</span><span style="font-family: FangSong_GB2312; font-size: 12px;">加上</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰后,<u>如果函数体内的语句试图改动strSource的内容,编译器将指出错误。</u></span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">如果输入参数采用“值传递”,由于函数将自动产生临时变量用于复制该参数,该输入参数本来就无需保护,所以不要加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">例如不要将函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc1(int x) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">写成</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc1(const int x)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。同理不要将函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc2(A a) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">写成</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc2(const A a)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。其中</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A</span><span style="font-family: FangSong_GB2312; font-size: 12px;">为用户自定义的数据类型。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">对于非内置数据类型作为参数而言,象</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(A a) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">这样声明的函数注定效率比较底。因为函数体内将产生</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A</span><span style="font-family: FangSong_GB2312; font-size: 12px;">类型的临时对象用于复制参数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">a</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,而临时对象的构造、复制、析构过程都将消耗时间。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"><u>为了提高效率,可以将函数声明改为voidFunc(A &a),因为“引用传递”仅借用一下参数的别名而已,不需要产生临时对象。但是函数voidFunc(A &a) 存在一个缺点:</u></span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">“</span><span style="font-family: FangSong_GB2312; font-size: 12px;">引用传递”有可能改变参数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">a</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,这是我们不期望的。解决这个问题很容易,加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰即可,因此函数最终成为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(const A &a)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">以此类推,是否应将</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(int x) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">改写为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(const int&x)(对于const修饰的引用,其实也是单向的,不能输出,这个输出的意思并不是简单的cout输出,而是通过输出形参“带回”结果)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,以便提高效率?</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">完全没有必要,因为内部数据类型的参数不存在构造、析构的过程,而复制也非常快,“值传递”和“引用传递”的效率几乎相当。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">问题是如此的缠绵,我只好将“</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const&”</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰输入参数的用法总结一下。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px; color: rgb(255, 0, 0);">对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const</span><span style="font-family: FangSong_GB2312; font-size: 12px; color: rgb(255, 0, 0);">引用传递”,目的是提高效率</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。例如将</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(A a) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">改为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(const A &a)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">对于内部数据类型的输入参数,不要将“值传递”的方式改为“</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">引用传递”。否则既达不到提高效率的目的,又降低了函数的可理解性。例如</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(int x) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">不应该改为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">voidFunc(const int &x)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"> 2</span><span style="font-family: FangSong_GB2312; font-size: 12px;"><span style="background-color: rgb(255, 255, 0);">用const</span><span style="background-color: rgb(255, 255, 0);">修饰函数的返回值 </span></span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 如果给以“指针传递”方式的函数返回值加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const修饰的同类型指针。例如函数 </span><span style="font-family: FangSong_GB2312; font-size: 12px;">const char * GetString(void); </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 如下语句将出现编译错误:</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> char *str = GetString(); </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 正确的用法是</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> const char *str =GetString(); </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 如果函数返回值采用“值传递方式”,由于函数会把返回值复制到外部临时的存储单元中,加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰没有任何价值。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 例如不要把函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">int GetInt(void) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">写成</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const int GetInt(void)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 同理不要把函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A GetA(void) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">写成</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const A GetA(void)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,其中</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A</span><span style="font-family: FangSong_GB2312; font-size: 12px;">为用户自定义的数据类型。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 如果返回值不是内部数据类型,将函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A GetA(void) </span><span style="font-family: FangSong_GB2312; font-size: 12px;">改写为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const A &GetA(void)</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的确能提高效率。但此时千万千万要小心,一定要搞清楚函数究竟是想返回一个对象的“拷贝”还是仅返回“别名”就可以了,否则程序会出错。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 函数返回值采用“引用传递”的场合并不多,这种方式一般只出现在类的赋值函数中,目的是为了实现链式表达。</span></strong>
<span style="font-weight: bold;"><span style="font-family: FangSong_GB2312; font-size: 12px;"> 例如:</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> class A { A & operate = (const A &other); // </span><span style="font-family: FangSong_GB2312; font-size: 12px;">赋值函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> }; A a, b, c; // a, b, c </span><span style="font-family: FangSong_GB2312; font-size: 12px;">为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的对象</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> a = b = c; // </span><span style="font-family: FangSong_GB2312; font-size: 12px;">正常的链式赋值,如果返回值不采用“引用传递”的话,链式赋值时会报错</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> (a = b) = c; // </span><span style="font-family: FangSong_GB2312; font-size: 12px;">不正常的链式赋值,但合法</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> 如果将赋值函数的返回值加</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰,那么该返回值的内容不允许被改动。上例中,语句</span><span style="font-family: FangSong_GB2312; font-size: 12px;">a= b = c </span><span style="font-family: FangSong_GB2312; font-size: 12px;">仍然正确,但是语句</span><span style="font-family: FangSong_GB2312; font-size: 12px;">(a= b) = c </span><span style="font-family: FangSong_GB2312; font-size: 12px;">则是非法的。<span style="font-family: 'AR PL UMing HK', serif;"> <span style="background-color: rgb(255, 255, 0);"> 3 const </span></span><span style="background-color: rgb(255, 255, 0);">成员函数</span></span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px; color: rgb(255, 0, 0);"> 任何不会修改数据成员的函数都应该声明为const</span><span style="font-family: FangSong_GB2312; font-size: 12px; color: rgb(255, 0, 0);">类型。</span><span style="font-family: FangSong_GB2312; font-size: 12px;">如果在编写</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数时,不慎修改了数据成员,或者调用了其它非</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数,编译器将指出错误,这无疑会提高程序的健壮性。以下程序中,类</span><span style="font-family: FangSong_GB2312; font-size: 12px;">stack</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的成员函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">GetCount</span><span style="font-family: FangSong_GB2312; font-size: 12px;">仅用于计数,从逻辑上讲</span><span style="font-family: FangSong_GB2312; font-size: 12px;">GetCount</span><span style="font-family: FangSong_GB2312; font-size: 12px;">应当为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数。编译器将指出</span><span style="font-family: FangSong_GB2312; font-size: 12px;">GetCount</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数中的错误。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> classStack { public: void Push(int elem); int Pop(void); int GetCount(void) const; // const </span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> private: int m_num; int m_data[100]; }; int Stack::GetCount(void) const { ++ m_num; // </span><span style="font-family: FangSong_GB2312; font-size: 12px;">编译错误,企图修改数据成员</span><span style="font-family: FangSong_GB2312; font-size: 12px;">m_num Pop();// </span><span style="font-family: FangSong_GB2312; font-size: 12px;">编译错误,企图调用非</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> return m_num; } const </span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数的声明看起来怪怪的:</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">关键字只能放在函数声明的尾部,大概是因为其它地方都已经被占用了。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span></span>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"> 关于</span><span style="font-family: FangSong_GB2312; font-size: 12px;">Const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数的几点规则:</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> a. const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">对象只能访问</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">而非</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">对象可以访问任意的成员函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">包括</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数</span><span style="font-family: FangSong_GB2312; font-size: 12px;">. b. const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">对象的成员是不可修改的</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">然而</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">对象通过指针维护的对象(该指针维护的对象是const对象的成员属性)却是可以修改的</span><span style="font-family: FangSong_GB2312; font-size: 12px;">. c. const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数不可以修改对象的数据</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">不管对象是否具有</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">性质</span><span style="font-family: FangSong_GB2312; font-size: 12px;">.</span><span style="font-family: FangSong_GB2312; font-size: 12px;">它在编译时</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">以是否修改成员数据为依据</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">进行检查</span><span style="font-family: FangSong_GB2312; font-size: 12px;">. e. </span><span style="font-family: FangSong_GB2312; font-size: 12px;">然而加上</span><span style="font-family: FangSong_GB2312; font-size: 12px;">mutable</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰符的数据成员</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">对于任何情况下通过任何手段都可修改</span><span style="font-family: FangSong_GB2312; font-size: 12px;">,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">自然此时的</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">成员函数是可以修改它的</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">补充:</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">标题</span><span style="font-family: FangSong_GB2312; font-size: 12px;">:const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">放在后面有什么意思?</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">--------------------------------------------------------------------------------</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">放在后面跟前面有区别么</span></strong>
<strong><span style="font-family: FangSong_GB2312; font-size: 12px;">==> </span><span style="font-family: FangSong_GB2312; font-size: 12px;">准确的说</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是修饰</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">指向的对象的</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;">譬如,我们定义了</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> class A{ public: f(int); }; </span><span style="font-family: FangSong_GB2312; font-size: 12px;">这里</span><span style="font-family: FangSong_GB2312; font-size: 12px;">f</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数其实有两个参数,第一个是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">A * const this, </span><span style="font-family: FangSong_GB2312; font-size: 12px;">另一个才是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">int</span><span style="font-family: FangSong_GB2312; font-size: 12px;">类型的参数</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> </span><span style="font-family: FangSong_GB2312; font-size: 12px;">如果我们不想</span><span style="font-family: FangSong_GB2312; font-size: 12px;">f</span><span style="font-family: FangSong_GB2312; font-size: 12px;">函数改变参数的值,可以把函数原型改为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">f(const int),</span><span style="font-family: FangSong_GB2312; font-size: 12px;">但如果我们不允许</span><span style="font-family: FangSong_GB2312; font-size: 12px;">f</span><span style="font-family: FangSong_GB2312; font-size: 12px;">改变</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">指向的对象呢?因为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是隐含参数,</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">没法直接修饰它,就加在函数的后面了,表示</span><span style="font-family: FangSong_GB2312; font-size: 12px;">this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">的类型是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const A * const this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">。</span><span style="font-family: FangSong_GB2312; font-size: 12px;"> const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">修饰</span><span style="font-family: FangSong_GB2312; font-size: 12px;">* this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是本质,至于说“表示该成员函数不会修改类的数据。否则会编译报错”之类的说法只是一个现象,根源就是因为</span><span style="font-family: FangSong_GB2312; font-size: 12px;">* this</span><span style="font-family: FangSong_GB2312; font-size: 12px;">是</span><span style="font-family: FangSong_GB2312; font-size: 12px;">const</span><span style="font-family: FangSong_GB2312; font-size: 12px;">类型的</span></strong>