關於const修飾符

 1.const與define的區別:

   const修飾的只讀變量具有特定的類型。

     define非關鍵字,只是簡單的字符替換,無類型可言。

  2.const修飾指針,變量

    const int i  /int const i  ‘近水樓臺先得月’ 變量i不能賦其他值,

    const int *p  變量*p對象不能改變,

    int * const p 指針p不能指向其他地址。

    const int *const p, *p不能被賦其他值,p不能指向其他地址。

  3.const 修飾返回值的時候

     如 const int & func();

    返回的對象不能更改

   4const 修飾函數參數的時候

     如  int func(const int i)

     函數體內不能對參數i進行修改。

  5.const *p與 *p:

   如  const char *p1;

       char *p2;

        char c='c';

   (1)p1=&c;                                                     (2)p2=&c;                                    

      p2=p1;                                                         p1=p2;

     編譯出錯,類型不匹配,但是可強轉              編譯通過,說明const更高級

 

   

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章