const參數,const傳回值與const函數

來源:互聯網
上載者:User

   在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     對

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.