欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中const的特性的使用

 更新時間:2020年05月07日 09:00:50   作者:怎因一雙媚眼惹塵埃  
這篇文章主要介紹了C++中const的特性的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

目錄(作用):

  1:修飾變量,說明該變量不可以被改變;
  2:修飾指針,分為只想常量的指針和自身是常量的指針
  3:修飾引用,指向常量的引用,用于修飾形參,即避免了拷貝,有避免了函數(shù)對值的修改; 
  4:修改成員函數(shù):說明該成員函數(shù)內(nèi)不能修改成員變量。
  5:指針與引用

正文:

以下是對各種情況的示例:

//注:1:const修飾的引用cj的值且引用的對象無法修改無法修改,但是引用的i是可修改的
#include <iostream>

using namespace std;

int main() {
  int i = 1;
  const int &cj = i;
  
  cout << "cj : " <<cj<< endl;(√)
  
  
  i=2;
  cout << "cj : " <<cj<< endl;(√)
  
  cj=3;
  cout << "cj : " <<cj<< endl;(×)
  
  int a=9;
  cj=a; (×)
  
  return 0;
}



錯誤提示:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:15:4: error: assignment of read-only reference ‘cj'
cj=3;
^
/code/main.cpp:19:4: error: assignment of read-only reference ‘cj'
cj=a;
^
sandbox> exited with status 0
//注:常量引用,本身也要是常量才行:

#include <iostream>

using namespace std;

int main() {
  const int i = 4;
 
  const int &ck = i; //正確,常量對象綁定到 const引用
   cout<< "ck="<<ck<<endl;
  
  const int b = 5;
 
  int &r = b;  //錯誤,
  
  return 0;
}



/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:14: error: invalid initialization of reference of type ‘int&' from expression of type ‘const int'
  int &r = b;  //錯誤,
       ^
sandbox> exited with status 0
//注:const 的隱式轉(zhuǎn)換:

#include <iostream>

using namespace std;

int main() {
  double b = 2.14;
  const int &a = b;
  // 會進(jìn)行如下轉(zhuǎn)換:
//   int temp = b;
//   const int &a=temp;
  // 所以,給b進(jìn)行賦值,a可能
  cout<<"a="<<a<<endl;
  return 0;
}

運(yùn)行結(jié)果:
a=2
sandbox> exited with status 0
//注:修飾成員函數(shù)_1:

class Date
{
  private:
  int m_year;
  int m_month;
  int m_day;
  public:
  int GetDay(void) const
  {
    m_day=7;
    return m_day;//修飾的情況下,不能對成員變量進(jìn)行修改;
  }
};

// void GetDay(void) const
// {
//   return m_day;

// }

int main() {
  double b = 2.14;
  const int &a = b;
  // 會進(jìn)行如下轉(zhuǎn)換:
  //   int temp = b;
  //   const int &a=temp;
  // 所以,給b進(jìn)行賦值,a可能
  cout<<"a="<<a<<endl;
  return 0;
}


錯誤提示:
/code/main.cpp: In member function ‘int Date::GetDay() const':
/code/main.cpp:16:8: error: assignment of member ‘Date::m_day' in read-only object
 m_day=7;
    ^
sandbox> exited with status 0
//注:修飾函數(shù)_2

#include <iostream>

  using namespace std;



class Date
{
  private:
  int m_year;
  int m_month;
   mutable int m_day;//通過被mutable修改的成員變量,就可以被修改了
  public:
  int GetDay(void) const
  {
    m_day=7;
    return m_day;
  }
};

// void GetDay(void) const
// {
//   return m_day;

// }

int main() {
  double b = 2.14;
  const int &a = b;
  // 會進(jìn)行如下轉(zhuǎn)換:
  //   int temp = b;
  //   const int &a=temp;
  // 所以,給b進(jìn)行賦值,a可能
  cout<<"a="<<a<<endl;
  return 0;
}


運(yùn)行結(jié)果:
a=2
sandbox> exited with status 0
//注:const修飾的指針


#include <iostream>

  using namespace std;


int main() {
  const int* p = NULL;//這兩種修飾的是*p指向的值
  //int const* p = NULL;

  int a=9;
  p=&a;//修改了p指向的地址,任然沒有出錯
  cout<<"*p="<<*p<<endl<<"p="<<p<<endl;
  
  
  int c=10;
  int* const b = &c;//這兩種修飾的是p指向的地址
   c=45;
  *b=c;//修改了b指向的值,任然不會出錯
  cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
  
  b=&a;//這里有問題了,b指向的地址是不能修改的
  cout<<"*b="<<*b<<endl<<"b="<<b<<endl;
  return 0;
}

運(yùn)行結(jié)果:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:21:3: error: assignment of read-only variable ‘b'
 b=&a;
  ^
sandbox> exited with status 0
//注:const修飾的引用

#include <iostream>

  using namespace std;


int main() {
  int x = 3;
  const int& y = x;
  cout<<"y="<<y<<endl;
  x=9;
  cout<<"y="<<y<<endl;
  
  y=9;//const修飾的引用是不能夠在更改引用指向的對象的
  cout<<"y="<<y<<endl;
  return 0;
}


運(yùn)行結(jié)果:
/code/main.cpp: In function ‘int main()':
/code/main.cpp:13:3: error: assignment of read-only reference ‘y'
 y=9;
  ^
sandbox> exited with status 0

到此這篇關(guān)于C++中const的特性的使用的文章就介紹到這了,更多相關(guān)C++ const的特性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++編程語言實(shí)現(xiàn)單鏈表詳情

    C++編程語言實(shí)現(xiàn)單鏈表詳情

    這篇文章主要介紹的是利用C語言實(shí)現(xiàn)單鏈表,實(shí)現(xiàn)的是鏈表中最簡單的一種單鏈表且每個結(jié)點(diǎn)中只含有一個指針域,下面將詳細(xì)舉例說明,需要的朋友可以參考一下
    2021-10-10
  • C語言?struct結(jié)構(gòu)體超詳細(xì)講解

    C語言?struct結(jié)構(gòu)體超詳細(xì)講解

    C語言中,結(jié)構(gòu)體類型屬于一種構(gòu)造類型(其他的構(gòu)造類型還有:數(shù)組類型,聯(lián)合類型),下面這篇文章主要給大家介紹了關(guān)于C語言結(jié)構(gòu)體(struct)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • C語言超詳細(xì)講解指針與結(jié)構(gòu)體

    C語言超詳細(xì)講解指針與結(jié)構(gòu)體

    指針提供了對地址操作的一種方法,因此,使用指針可使得C語言能夠更高效地實(shí)現(xiàn)對計算機(jī)底層硬件的操作。另外,通過指針可以更便捷地操作數(shù)組。C數(shù)組允許定義可存儲相同類型數(shù)據(jù)項的變量,結(jié)構(gòu)是C編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許您存儲不同類型的數(shù)據(jù)項
    2022-05-05
  • Qt 使用 canon edsdk 實(shí)現(xiàn)實(shí)時預(yù)覽的示例代碼

    Qt 使用 canon edsdk 實(shí)現(xiàn)實(shí)時預(yù)覽的示例代碼

    這篇文章主要介紹了Qt 使用 canon edsdk 實(shí)現(xiàn)實(shí)時預(yù)覽的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C++友元函數(shù)與拷貝構(gòu)造函數(shù)詳解

    C++友元函數(shù)與拷貝構(gòu)造函數(shù)詳解

    這篇文章主要介紹了C++友元函數(shù)與拷貝構(gòu)造函數(shù),需要的朋友可以參考下
    2014-07-07
  • 關(guān)于STL中l(wèi)ist容器的一些總結(jié)

    關(guān)于STL中l(wèi)ist容器的一些總結(jié)

    list就是數(shù)據(jù)結(jié)構(gòu)中的雙向鏈表(根據(jù)sgi stl源代碼),因此它的內(nèi)存空間是不連續(xù)的,通過指針來進(jìn)行數(shù)據(jù)的訪問,這個特點(diǎn)使得它的隨即存取變的非常沒有效率,因此它沒有提供[]操作符的重載
    2013-09-09
  • C++超詳細(xì)講解逗號操作符

    C++超詳細(xì)講解逗號操作符

    使用逗號運(yùn)算符是為了把幾個表達(dá)式放在一起。整個逗號表達(dá)式的值為系列中最后一個表達(dá)式的值。從本質(zhì)上講,逗號的作用是將一系列運(yùn)算按順序執(zhí)行
    2022-05-05
  • c++ #include是怎么樣工作的?

    c++ #include是怎么樣工作的?

    大多數(shù)園友可能對“#include”比較熟悉,因為我們寫C/C++程序的時候都會寫的字符串之一,但是它是具體怎么工作的?或者它的原理是什么呢?
    2013-01-01
  • 用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法詳解

    用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法詳解

    本篇文章是對用C++實(shí)現(xiàn),將一句話里的單詞進(jìn)行倒置的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • __stdcall 和 __cdecl 的區(qū)別淺析

    __stdcall 和 __cdecl 的區(qū)別淺析

    __stdcall 和 __cdecl 的區(qū)別淺析,需要的朋友可以參考一下
    2013-03-03

最新評論