前言
? ? ? ? 本篇幅講解關(guān)鍵字const與static,主要圍繞在類(lèi)的范圍內(nèi)敘述,包括作用和使用場(chǎng)景等。
一、const與static的作用
? ? ? ? 1、const修飾的成員變量,成員變量初始化后不能再修改。
? ? ? ? 2、const修飾的成員函數(shù),成員函數(shù)不可以修改成員變量,也不能間接修改。
? ? ? ? 3、static修飾的成員變量,變?yōu)殪o態(tài)成員變量,只能在類(lèi)外初始化,由靜態(tài)成員函數(shù)來(lái)操作。
? ? ? ? 4、static修飾的成員函數(shù),變?yōu)殪o態(tài)成員函數(shù),用來(lái)操作靜態(tài)成員變量,不能訪(fǎng)問(wèn)非靜態(tài)成員變量和非靜態(tài)成員函數(shù)。
二、使用場(chǎng)景
? ? ? ? 1、const修飾成員變量,通常用于修飾一經(jīng)初始化后,不能修改的變量(如學(xué)號(hào),姓名)。
? ? ? ? 2、const修飾成員函數(shù),通常用于返回成員變量的函數(shù)。
? ? ? ? 3、static修飾成員變量,通常用于所有類(lèi)對(duì)象共享的變量,該靜態(tài)成員變量在類(lèi)中只有一個(gè)副本,
? ? ? ? 4、static修飾成員函數(shù),通常用于需要操作靜態(tài)變量的成員函數(shù)。
三、案例
? ? ? ? 1、const修飾成員變量
#include <iostream> using namespace std; class Test { private: int a; const int b; public: // const 修飾的成員變量采用構(gòu)造函數(shù)初始化列表的方式 Test(int a = 0, const int b = 0):b(b) { this->a = a; // this->b = b; // 上面已經(jīng)初始化了,這里不允許再次初始化 } void show() { cout << "a = " << a << endl; cout << "b = " << b << endl; } }; int main() { Test A(1, 10); A.show(); return 0; }
????????2、const修飾成員函數(shù)
#include <iostream> using namespace std; class Test { private: int a; const int b; public: // const 修飾的成員變量采用構(gòu)造函數(shù)初始化列表的方式 Test(int a = 0, const int b = 0):b(b) { this->a = a; // this->b = b; // 上面已經(jīng)初始化了,這里不允許再次初始化 } // 獲取成員變量的函數(shù)通常用const修飾 int getA()const { return a; } int getB()const { return b; } }; int main() { Test A(1, 10); cout << "A = " << A.getA() << endl; cout << "B = " << A.getB() << endl; return 0; }
????????3、static修飾成員變量和成員函數(shù)
#include <iostream> using namespace std; class Test { private: int a; const int b; static int c; // 靜態(tài)成員變量 public: // const 修飾的成員變量采用構(gòu)造函數(shù)初始化列表的方式 Test(int a = 0, const int b = 0):b(b) { this->a = a; c++; // this->b = b; // 上面已經(jīng)初始化了,這里不允許再次初始化 } // 不能寫(xiě)成: static int getC()const, 靜態(tài)成員函數(shù)不允許使用類(lèi)型限定符 static int getC() { return c; } }; // 靜態(tài)成員變量在類(lèi)中聲明,類(lèi)外定義,定義時(shí)不需要加static int Test::c = 0; int main() { Test A(1, 10); // 可以使用對(duì)象名.靜態(tài)成員函數(shù)名來(lái)訪(fǎng)問(wèn) cout << "C = " << A.getC() << endl; Test B(2, 20); cout << "C = " << B.getC() << endl; // 可以使用類(lèi)名::靜態(tài)成員函數(shù)名來(lái)訪(fǎng)問(wèn) cout << "C = " << Test::getC() << endl; return 0; }
????????4、const和static共同修飾成員變量
#include <iostream> using namespace std; class Test { private: int a; const int b; const static int c; // 用const來(lái)修飾靜態(tài)成員變量 public: // const 修飾的成員變量采用構(gòu)造函數(shù)初始化列表的方式 Test(int a = 0, const int b = 0):b(b) { this->a = a; // c++; // 靜態(tài)成員變量不能修改 // this->b = b; // 上面已經(jīng)初始化了,這里不允許再次初始化 } // 不能寫(xiě)成: static int getC()const, 靜態(tài)成員函數(shù)不允許使用類(lèi)型限定符 static int getC() { return c; } }; // 靜態(tài)成員變量在類(lèi)中聲明,類(lèi)外定義,定義時(shí)不需要加static const int Test::c = 0; int main() { Test A(1, 10); // 可以使用對(duì)象名.靜態(tài)成員函數(shù)名來(lái)訪(fǎng)問(wèn) cout << "C = " << A.getC() << endl; Test B(2, 20); cout << "C = " << B.getC() << endl; // 可以使用類(lèi)名::靜態(tài)成員函數(shù)名來(lái)訪(fǎng)問(wèn) cout << "C = " << Test::getC() << endl; return 0; }
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-609657.html
四、總結(jié)
? ? ? ? const與static關(guān)鍵字都可以修飾成員變量和函數(shù),但是作用有所不同。如果是獲取非靜態(tài)成員變量的返回值的函數(shù),通常用const來(lái)修飾,增加可讀性。如果是用static來(lái)修飾成員變量,該成員變量變?yōu)殪o態(tài)成員變量,不屬于任何類(lèi)對(duì)象,只屬于類(lèi),只有一個(gè)副本,類(lèi)對(duì)象成員之間共享。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-609657.html
到了這里,關(guān)于重學(xué)C++系列之const與static關(guān)鍵字分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!