const参数,const返回值与const函数
在C++程序中,經常用const 來限制對一個對象的操作,例如,將一個變量定義為const 的:
const? int? n=3;
則這個變量的值不能被修改,即不能對變量賦值。
?????? const 這個關鍵字經常出現在函數的定義中,而且會出現在不同的位置,比如:
?????????????? int? strcmp (const? char? *str1,const?? char? *str2);
????????????? const?? int? & min (int? &, int? &);
????????????? void? printMessage (char? *msg) const;
1.const 參數
?????? 出現在函數參數中的const 表示在函數體中不能對這個參數做修改。比如上面的例子中strcmp() 函數用來比較兩個字符串的大小,在函數體中不應該改變兩個參數的值,所以將它定義為是const 的。const 通常用來限制函數的指針參數,引用和數組參數,而一般形式的參數因為形參和實參本來就不在同一內存空間,所以對形參的修改不會影響實參,因此也沒有必要限制函數體不能對參數進行修改。
?
??????? 下面是一些使用函數 const 參數的例子:
(1)? 函數 strcpy() 將 src 字符串的內容復制到 targ 字符串中,為保證 src 字符串不被修改,將它定義為 const 參數:
?????????????? void? strcpy ( const? char? *src , char? * targ);
(2)? 函數 max() 從數組 array 中找出具有最大值的數組元素并返回這個最大元素的值,為保證數組元素不會在函數中被修改, 將它定義為 const? 參數:
????????????? int? max ( const? int? array[ ],? int? size);
(3)? 函數 outputObject( ) 將類 Myclass 的對象 obj 的內容輸出。對象定義為 const? 引用,即可以保證對象不會在函數體中有所改變,又可以節省對象傳遞的開銷:
????????????? void? outputObject ( const?? Myclass? &obj) ;
PS:
?????? const 指針可以接受const 和非 const 地址,但是非const 指針只能接受非const 地址。所以const? 指針的能力更強一些,所以盡量多用const 指針,這是一種習慣。
2. const 返回值
??????? 函數返回值為 const? 只有用在函數返回為引用的情況。 函數返回值引用常量表示不能將函數調用表達式作為左值使用。例如前面講的返回引用的函數 min( )。
??? ??? int? & min ( int? &i,? int? &j);?
可以對函數調用進行賦值,因為它返回的是左值:? min ( a ,? b )=4;
但是,如果對函數的返回值限定為 const? 的:const? int? & min ( int & i, int? &j );
那么,就不能對 min ( a, b ) 調用進行賦值了。
3. const 函數
???????? 在類中,可以為類的成員函數進行如下形式的定義:
class? classname {
????????? int? member ;
? public:
???????? int? getMember ( ) const;
};
?????? 這里,在函數定義頭后面加上的 const 表示這個函數是一個“只讀函數”,函數不能改變類對象的狀態,不能改變對象的成員變量的值。如在函數體中不能這么寫:
??? classname :: getmember( )
?? {? member =4 ;?
???? return? member;
?? }
?
另外,const成員函數也不能在函數中調用其他非const 的函數。______________________________________________________________________________
補充:
以下內容轉載于: http://www.chinaunix.net/jh/23/300602.html
以下面的例子為例進行說明:
#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 );
}
??
首先說一點題外話,為什么 get_name( ) 前面也加 const。如果沒有前后兩個 const 的話,get_name() 返回的是對私有數據成員 name 的引用,所以通過這個引用可以改變私有成員 name 的值,如:
? Student stu( "Wang", 85 );
? stu.get_name() = "Li";????? //? 引用可以作為左值
即把 name 由原來的 "Wang" 變成了 "Li",而這不是我們希望的發生的。所以在 get_name() 前面加 const 避免這種情況的發生。
?
那么,get_name( ) 和 get_score( ) 這兩個后面應該加 const 的成員函數,如果沒有 const 修飾的話可不可以呢?回答是可以!但是這樣做的代價是:const 對象將不能再調用這兩個非const成員函數了。如:
const string& get_name( );
int get_score( );??????? // 這兩個函數都應該設成 const 型
void output_student( const Student& student )
? {
? cout << student.get_name() << "/t";
? cout << student.get_score() << endl;
// 如果 get_name() 和 get_score() 是非const 成員函數,這兩句調用都是錯誤的
}
?
由于參數 student 表示的是一個對const Student 型對象的引用,所以 student 不能調用非const 成員函數如 set_student(? )。如果 get_name() 和 get_score() 成員函數也變成非const 型,那么上面的 student.get_name() 和 student.get_score() 的使用就是非法的,這樣就會給我們處理問題造成困 難。
因此,我們沒有理由反對使用const,該加const 時就應該加上const,這樣使成員函數除了非const 的對象之外,const 對象也能夠調用它。
?
對象.成員函數
???????? 對象????????? 成員函數?????? 對/錯
1、? const??????????? const????????????? 對
2、 const?????????? non-const???? ??? 錯
3、? non-const???? const?????????? ?? 對
4、? not-const???? non-const???? ?? 對
????????? 成員函數調用成員函數
???? 成員函數????? 成員函數?????? 對/錯
5、? const??????????? const???????????? 對
6、? const???????? non-const???????? 錯
7、? non-const???? const?????????? ? 對
8、? non-const???? non-const????? 對
轉載于:https://www.cnblogs.com/guanqingbuyu/p/5562866.html
總結
以上是生活随笔為你收集整理的const参数,const返回值与const函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机APP测试需要注意的问题
- 下一篇: php方法-------将汉字转为拼音或