日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

const参数,const返回值与const函数

發(fā)布時(shí)間:2023/12/20 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 const参数,const返回值与const函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在C++程序中,經(jīng)常用const 來限制對一個(gè)對象的操作,例如,將一個(gè)變量定義為const 的:

const? int? n=3;

則這個(gè)變量的值不能被修改,即不能對變量賦值。

?????? const 這個(gè)關(guān)鍵字經(jīng)常出現(xiàn)在函數(shù)的定義中,而且會出現(xiàn)在不同的位置,比如:

?????????????? int? strcmp (const? char? *str1,const?? char? *str2);

????????????? const?? int? & min (int? &, int? &);

????????????? void? printMessage (char? *msg) const;

1.const 參數(shù)

?????? 出現(xiàn)在函數(shù)參數(shù)中的const 表示在函數(shù)體中不能對這個(gè)參數(shù)做修改。比如上面的例子中strcmp() 函數(shù)用來比較兩個(gè)字符串的大小,在函數(shù)體中不應(yīng)該改變兩個(gè)參數(shù)的值,所以將它定義為是const 的。const 通常用來限制函數(shù)的指針參數(shù),引用和數(shù)組參數(shù),而一般形式的參數(shù)因?yàn)樾螀⒑蛯?shí)參本來就不在同一內(nèi)存空間,所以對形參的修改不會影響實(shí)參,因此也沒有必要限制函數(shù)體不能對參數(shù)進(jìn)行修改。

?

??????? 下面是一些使用函數(shù) const 參數(shù)的例子:

(1)? 函數(shù) strcpy() 將 src 字符串的內(nèi)容復(fù)制到 targ 字符串中,為保證 src 字符串不被修改,將它定義為 const 參數(shù):

?????????????? void? strcpy ( const? char? *src , char? * targ);

(2)? 函數(shù) max() 從數(shù)組 array 中找出具有最大值的數(shù)組元素并返回這個(gè)最大元素的值,為保證數(shù)組元素不會在函數(shù)中被修改, 將它定義為 const? 參數(shù):

????????????? int? max ( const? int? array[ ],? int? size);

(3)? 函數(shù) outputObject( ) 將類 Myclass 的對象 obj 的內(nèi)容輸出。對象定義為 const? 引用,即可以保證對象不會在函數(shù)體中有所改變,又可以節(jié)省對象傳遞的開銷:

????????????? void? outputObject ( const?? Myclass? &obj) ;

PS:

?????? const 指針可以接受const 和非 const 地址,但是非const 指針只能接受非const 地址。所以const? 指針的能力更強(qiáng)一些,所以盡量多用const 指針,這是一種習(xí)慣。

2. const 返回值

??????? 函數(shù)返回值為 const? 只有用在函數(shù)返回為引用的情況。 函數(shù)返回值引用常量表示不能將函數(shù)調(diào)用表達(dá)式作為左值使用。例如前面講的返回引用的函數(shù) min( )。

??? ??? int? & min ( int? &i,? int? &j);?

可以對函數(shù)調(diào)用進(jìn)行賦值,因?yàn)樗祷氐氖亲笾?#xff1a;? min ( a ,? b )=4;

但是,如果對函數(shù)的返回值限定為 const? 的:const? int? & min ( int & i, int? &j );

那么,就不能對 min ( a, b ) 調(diào)用進(jìn)行賦值了。

3. const 函數(shù)

???????? 在類中,可以為類的成員函數(shù)進(jìn)行如下形式的定義:

class? classname {

????????? int? member ;

? public:

???????? int? getMember ( ) const;

};

?????? 這里,在函數(shù)定義頭后面加上的 const 表示這個(gè)函數(shù)是一個(gè)“只讀函數(shù)”,函數(shù)不能改變類對象的狀態(tài),不能改變對象的成員變量的值。如在函數(shù)體中不能這么寫:

??? classname :: getmember( )

?? {? member =4 ;?

???? return? member;

?? }

?

另外,const成員函數(shù)也不能在函數(shù)中調(diào)用其他非const 的函數(shù)。______________________________________________________________________________

補(bǔ)充:

以下內(nèi)容轉(zhuǎn)載于: http://www.chinaunix.net/jh/23/300602.html

以下面的例子為例進(jìn)行說明:

#include <iostream>;

#include <string>;

using namespace std;

class Student {

??????? string name;

??????? int score;

public:

??? Student ( ) { }?

??? Student ( const string& nm, int sc = 0 )? : name( nm ), score( sc ) { }

???

??? void set_student( const string& nm, int sc = 0 )?? // 后面不能有const

??????? {???? name = nm;???????? score = sc;??????? }

??? const string& get_name() const

?????? {???? return name;?? }

?? int get_score() const

?????? {???? return score;?? }

};

// output student's name and score

void output_student( const Student& student )

{

? cout << student.get_name() << "/t";

? cout << student.get_score() << endl;

}

int main()

{

? Student stu( "Wang", 85 );

? output_student( stu );

}

??

首先說一點(diǎn)題外話,為什么 get_name( ) 前面也加 const。如果沒有前后兩個(gè) const 的話,get_name() 返回的是對私有數(shù)據(jù)成員 name 的引用,所以通過這個(gè)引用可以改變私有成員 name 的值,如:

? Student stu( "Wang", 85 );

? stu.get_name() = "Li";????? //? 引用可以作為左值

即把 name 由原來的 "Wang" 變成了 "Li",而這不是我們希望的發(fā)生的。所以在 get_name() 前面加 const 避免這種情況的發(fā)生。

?

那么,get_name( ) 和 get_score( ) 這兩個(gè)后面應(yīng)該加 const 的成員函數(shù),如果沒有 const 修飾的話可不可以呢?回答是可以!但是這樣做的代價(jià)是:const 對象將不能再調(diào)用這兩個(gè)非const成員函數(shù)了。如:

const string& get_name( );

int get_score( );??????? // 這兩個(gè)函數(shù)都應(yīng)該設(shè)成 const 型

void output_student( const Student& student )

? {

? cout << student.get_name() << "/t";

? cout << student.get_score() << endl;

// 如果 get_name() 和 get_score() 是非const 成員函數(shù),這兩句調(diào)用都是錯(cuò)誤的

}

?

由于參數(shù) student 表示的是一個(gè)對const Student 型對象的引用,所以 student 不能調(diào)用非const 成員函數(shù)如 set_student(? )。如果 get_name() 和 get_score() 成員函數(shù)也變成非const 型,那么上面的 student.get_name() 和 student.get_score() 的使用就是非法的,這樣就會給我們處理問題造成困 難。

因此,我們沒有理由反對使用const,該加const 時(shí)就應(yīng)該加上const,這樣使成員函數(shù)除了非const 的對象之外,const 對象也能夠調(diào)用它。

?

對象.成員函數(shù)
???????? 對象????????? 成員函數(shù)?????? 對/錯(cuò)

1、? const??????????? const????????????? 對

2、 const?????????? non-const???? ??? 錯(cuò)

3、? non-const???? const?????????? ?? 對

4、? not-const???? non-const???? ?? 對

????????? 成員函數(shù)調(diào)用成員函數(shù)
???? 成員函數(shù)????? 成員函數(shù)?????? 對/錯(cuò)

5、? const??????????? const???????????? 對

6、? const???????? non-const???????? 錯(cuò)

7、? non-const???? const?????????? ? 對

8、? non-const???? non-const????? 對

轉(zhuǎn)載于:https://www.cnblogs.com/guanqingbuyu/p/5562866.html

總結(jié)

以上是生活随笔為你收集整理的const参数,const返回值与const函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。