日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++const关键字作用

發布時間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++const关键字作用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

修飾普通變量,表示不可修改(在定義的時候必須初始化)

#include <iostream>
using namespace std;
const int a1 = 10;
int main()
{const int a3; // 錯誤,沒有初始化const int a2 = 10;a1 = 10;    // 錯誤,不可修改變量的值int b1 = a1; // 可以給其余變量賦值
}

修飾指針

const修飾指針時分3種情況:常量指針、指針常量、常指針常量。

常量指針

#include <iostream>
using namespace std;
int main()
{int a = 10;// 常量指針,指針指向對象的值不能變,指針指向的對象可以變const int* ptr = &a;*ptr = 100;  //錯誤int b = 100;ptr = &b; // 正確
}

指針常量

#include <iostream>
using namespace std;
int main()
{int a = 10;// 指針常量,指針指向的對象不可以變,指向對象的值可以變int* const ptr = &a;*ptr = 100; // 正確int b = 100;ptr = &b; // 錯誤
}

常指針常量

#include <iostream>
using namespace std;
int main()
{int a = 10;// 常指針常量,指針指向的對象、指向對象的值都不能變const int* const ptr = &a;*ptr = 100; // 正確int b = 100;ptr = &b; // 錯誤
}

const用于函數中

修飾函數的形參

表示函數中不能改變該形參的值。

#include <iostream>
using namespace std;
void function(const int a)
{cout<<a<<endl;a++;// 錯誤,a用const修飾,大小不能改變
}
int main()
{int a = 10;function(a);
}

修飾函數中的指針形參

防止意外修改指針指向對象的值。

#include <iostream>
using namespace std;
void function(const int* a,int* const b)
{int temp = 100;*a = 10;    // 錯誤a = &temp;  // 正確b = &temp;  // 錯誤*b = 20;    // 正確
}
int main()
{int a = 10;int b = 10;function(&a,&b);
}

修飾函數返回值

當函數返回值為int&類型時,可以作為左值
當函數返回值為const int&類型時,不可以作為左值

#include <iostream>
using namespace std;int& test01(int& a)
{return a;
}const int& test02(int& a)
{return a;
}
int main()
{// 函數返回值為int&類型時,可以作為左值進行賦值int a = 10;test01(a) = 100;cout << a << endl;// 函數返回值為const int&類型時,不可以作為左值進行賦值int b = 10;test02(b) = 100;}

const用于類中

修飾成員函數(常函數)

const修飾成員函數時,表示成員函數中成員變量不能修改。

#include <iostream>
using namespace std;
class test
{
public:test(int value):m_value(value){}test(){}~test(){}void print_value() const{//this->m_value = 100; // 錯誤,const修飾成員函數中,不能修改成員變量的值cout<<this->m_value<<endl;}const int get_value(){return this->m_value;}
private:int m_value;
};int main()
{test Test(20);int value = Test.get_value();cout<<value<<endl;
}

如果想讓const成員變量可以在成員函數中修改的話,可以加一個關鍵字mutable。

#include <iostream>
using namespace std;
class test
{
public:test(int value):m_value(value){}test(){}~test(){}void print_value() const{this->m_value = 100; // 此時可以,因為成員變量被mutable修飾cout<<this->m_value<<endl;}const int get_value(){return this->m_value;}
private:mutable int m_value;
};int main()
{test Test(20);int value = Test.get_value();Test.print_value();cout<<value<<endl;
}

修飾類對象(常對象)

常對象只能調用常成員函數。不能修改成員變量的值(除非有mutable修飾)

#include <iostream>
using namespace std;
class test
{
public:test(int value) :m_value(value) {}test() {}~test() {}void print_value() const{cout << this->m_value << endl;}int get_value(){return this->m_value;}int m_value;mutable int m_date;
};int main()
{const test test01(100);// 錯誤,不能修改常對象中的成員變量的值,除非加mutabletest01.m_value = 1000;// 可以,因為m_date類型為mutable inttest01.m_date = 2021;// 可以,常對象可以調用常函數test01.print_value();// 不可以,常對象只能調用常函數int val = test01.get_value();
}

參考文章:
https://www.runoob.com/w3cnote/cpp-const-keyword.html

總結

以上是生活随笔為你收集整理的C++const关键字作用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。