C++成員函數(shù)中const的使用詳解_第1頁
C++成員函數(shù)中const的使用詳解_第2頁
C++成員函數(shù)中const的使用詳解_第3頁
C++成員函數(shù)中const的使用詳解_第4頁
C++成員函數(shù)中const的使用詳解_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第C++成員函數(shù)中const的使用詳解目錄修飾入?yún)⒅祩鬟f址傳遞const修飾入?yún)⑿揎椃祷刂敌揎椇瘮?shù)總結(jié)const在C++中是一個很重要的關(guān)鍵字,其不光可以用來修飾變量,還可以放在函數(shù)定義中,這里整理了其在函數(shù)中的三個用法。

修飾入?yún)?/p>

首先我們要明白在C++中調(diào)用函數(shù)時存在兩種方法,即傳遞值和傳遞引用。

值傳遞

值傳遞時,調(diào)用函數(shù)時會創(chuàng)建入?yún)⒌目截悾瘮?shù)中的操作不會對原值進行修改,因此這種方式中不需要使用const來修飾入?yún)?,因為其只是對拷貝的臨時對象進行操作。

址傳遞

傳遞地址時函數(shù)中的操作實際上是直接對原來的值進行修改,因此我們這里可以使用const修飾入?yún)ⅰ?/p>

const修飾入?yún)?/p>

當const修飾函數(shù)入?yún)r表示該參數(shù)不能被修改,這個是最好理解的,比如一個函數(shù)的功能是拷貝,那么入?yún)⒅械脑次募紩胏onst修飾。

voidA::show(constint*b){

cout"showconst";

//error:read-onlyvariableisnotassignable

//*b=2;

coutbendl;

接下來我們要關(guān)注的是這里const對于函數(shù)重載的作用,這里給出結(jié)論,歡迎大家討論,對應(yīng)按值傳遞的函數(shù)來說const不會有重載的效果,但是傳遞指針和引用是會有重載的效果。

voidA::show(constintb)

//voidA::show(intb)//errorclassmembercannotberedeclared

voiddisplay(int*num);//overload

voiddisplay(constint*num);//overload

voidfun(A//overload

voidfun(constA//overload

函數(shù)重載的關(guān)鍵是函數(shù)的參數(shù)列表即函數(shù)特征標(functionsignature)。如果兩個函數(shù)的參數(shù)數(shù)目和類型相同,并且參數(shù)的排列順序也相同,則他們的特征標相同,而變量名是無關(guān)緊要的。

總結(jié)一下注意點:

如果輸入?yún)?shù)采用值傳遞,**由于函數(shù)將自動產(chǎn)生臨時變量用于復(fù)制該參數(shù),該輸入?yún)?shù)本來就無需保護,所以不要加const修飾。**例如不要將函數(shù)voidFunc1(intx)寫成voidFunc1(constintx)。如果參數(shù)作為輸出參數(shù),不論它是什么數(shù)據(jù)類型,也不論它采用指針傳遞還是引用傳遞,都不能加const修飾,否則該參數(shù)將失去輸出功能(因為有const修飾之后,不能改變他的值)。如果參數(shù)作為輸入?yún)?shù),可以防止數(shù)據(jù)被改變,起到保護作用,增加程序的健壯性,建議是能加const盡量加上

上述測試代碼如下:

#includeiostream

usingnamespacestd;

classA{

private:

inta;

public:

A(inta){

this-a=a;

voidshow(intb);

//errorredeclared

//voidshow(constintb);

voiddisplay(int*num);//ok

voiddisplay(constint*num);//ok

voidfun(A

voidfun(constA

voidhappy(int*h);

voidhour(constint*h);

voidA::show(intb){

cout"show:"bendl;

voidA::display(int*num){

cout"display:"*numendl;

voidA::display(constint*num){

cout"constdisplay:"*numendl;

voidA::fun(Aobj){

cout"fun:"obj.aendl;

voidA::fun(constAobj){

cout"constfun:"obj.aendl;

voidA::happy(int*h){

cout"happy:"*hendl;

voidA::hour(constint*h){

cout"consthour:"*hendl;

intmain(){

Aa(1);

constAa2(11);

intb1=2;

constintb2=3;

//testoverload

a.show(b1);

a.show(b2);

a.display(b1);

a.display(b2);

a.fun(a);

a.fun(a2);

//testconst

a.happy(b1);

//a.happy(b2);//errorcannotinitializeaparameteroftype'int*'withanrvalueoftype'constint*'

a.hour(b1);

a.hour(b2);

return0;

//ouptut

show:2

show:3

display:2

constdisplay:3

fun:1

constfun:11

happy:2

consthour:2

consthour:3

修飾返回值

const修飾返回值時,表示返回值不能被修改。需要注意的是如果函數(shù)返回值采用值傳遞方式,由于函數(shù)會把返回值復(fù)制到外部臨時的存儲單元中,加const修飾沒有任何價值。如果返回的是引用或指針,表示不能修改指向的數(shù)據(jù)。

一般用得多的是返回值是引用的函數(shù),可以肯定的是這個引用必然不是臨時對象的引用,因此一定是成員變量或者是函數(shù)參數(shù),所以在返回的時候為了避免其成為左值被修改,就需要加上const關(guān)鍵字來修飾。

我們可以看如下代碼示例:

#includeiostream

usingnamespacestd;

classAlice{

private:

inta;

public:

Alice(inta):a(a){}

intget_a(){returna;}

constint*get_const_ptr(){return}

int*get_ptr(){return}

intmain(){

Alicealice(1);

inta1=alice.get_a();//ok

couta1endl;

constinta2=alice.get_a();//ok

couta2endl;

//errorcannotinitializeavariableoftype'int*'withanrvalueoftype'constint*'

//int*b1=alice.get_const_ptr();

constint*b2=alice.get_const_ptr();//ok

cout*b2endl;//ok

//*b2=3;//errorread-onlyvariableisnotassignable

*(alice.get_ptr())=3;

coutalice.get_a()endl;//3

return0;

修飾函數(shù)

const也可以用來放在函數(shù)末尾,用來修飾成員函數(shù),表明其是一個常成員函數(shù),這個對于初次接觸C++的同學來說會有點陌生,不過這也是C++中嚴謹?shù)牡胤?。先看代碼示例,學習任何編程技術(shù)都一定要寫對應(yīng)的代碼,把它跑起來并分析結(jié)果才算是真正學會了,不會你只是知道了這個知識點,只知其然而不知其所以然。紙上得來終覺淺,絕知此事要躬行,這里的要躬行指的就是寫代碼。

首先來看如下的代碼

classAlice{

private:

inta;

public:

Alice(inta):a(a){}

voidshow();

voidAlice::show(){

cout"helloAlice"endl;

intmain(){

constAlicea(1);

//error:'this'argumenttomemberfunction'show'hastype'constAlice',butfunctionisnotmarkedconst

//a.show();

return0;

上述代碼會報錯,因為show()方法不是常成員函數(shù),而a是常對象。本質(zhì)上,成員函數(shù)中都有一個隱含的入?yún)his,這個this指的就是調(diào)用該方法的對象,而如果在函數(shù)的后面加上const,那么這個const實際上修飾的就是這個this。也就是說函數(shù)后加上了const,表明這個函數(shù)不會改變調(diào)用者對象。

這里借用侯捷老師的圖片

上面圖片表明,在正常情況下:

non-const對象可以調(diào)用const或者non-const成員函數(shù)const對象只可以調(diào)用const成員函數(shù)

補充一點,**如果成員函數(shù)同時具有const和non-const兩個版本的話,const對象只能調(diào)用const成員函數(shù),non-const對象只能調(diào)用non-const成員函數(shù)。**如以下代碼示例

#includeiostream

usingnamespacestd;

classR{

public:

R(intr1,intr2){

a=r1;

b=r2;

voidprint();

voidprint()const;

private:

inta;

intb;

voidR::print(){

cout"normalprint"endl;

couta","bendl;

voidR::print()const{

cout"c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論