C++基础07-类之静态成员变量和成员函数
總結(jié):
1、靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)屬于整個類而不是某個對象
?? 即使沒有定義對象,靜態(tài)成員也是存在的
2、static 成員類外存儲,求類大小,并不包含在內(nèi)。 (因為存儲在全局區(qū),而類一般存儲在棧區(qū))
3、靜態(tài)數(shù)據(jù)成員在類中說明,在類外定義 并且會給他分配內(nèi)存空間,并初始化(一經(jīng)定義則必須初始化)
4、靜態(tài)數(shù)據(jù)成員的生存周期一直存在于整個程序的生命周期
4、靜態(tài)成員函數(shù)只能訪問靜態(tài)數(shù)據(jù)成員(原因:非靜態(tài)成員函數(shù),在調(diào)用時this 指 針被當(dāng)作參數(shù)傳進。而靜態(tài)成員函數(shù)屬于類,而不屬于對象,沒有 this 指針)
5、靜態(tài)成員函數(shù)的意義,不在于信息共享,數(shù)據(jù)溝通,而在于管理靜態(tài)數(shù)據(jù)成員, 完成對靜態(tài)數(shù)據(jù)成員的封裝。
6、靜態(tài)成員函數(shù)可在類內(nèi)實現(xiàn)也可在類外
6、可以通過類作用域 ::? 訪問靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)
7、類,struct占用內(nèi)存,都采用字節(jié)對齊方式計算
通過上面的案例,我們可以的得出:
C++類對象中的成員變量和成員函數(shù)是分開存儲的
成員變量:
???????? 普通成員變量:存儲于對象中,與struct變量有相同的內(nèi)存布局和字節(jié)對齊方式
????????? 靜態(tài)成員變量:存儲于全局數(shù)據(jù)區(qū)中
成員函數(shù):存儲于代碼段中。
一、靜態(tài)數(shù)據(jù)成員的使用
//聲明
static 數(shù)據(jù)類型 成員變量; //在類的內(nèi)部
//初始化
數(shù)據(jù)類型 類名::靜態(tài)數(shù)據(jù)成員 = 初值; //在類的外部
//調(diào)?
類名::靜態(tài)數(shù)據(jù)成員
類對象.靜態(tài)數(shù)據(jù)成員
1,static 成員變量實現(xiàn)了同類對象間信息共享。
2,static 成員類外存儲,求類大小,并不包含在內(nèi)。
3,static 成員是命名空間屬于類的全局變量,存儲在 data 區(qū)。
4,static 成員只能類外初始化。
5,可以通過類名訪問(無對象生成時亦可),也可以通過對象訪問。
二、靜態(tài)成員函數(shù)的使用
//聲明
static 函數(shù)聲明
//調(diào)?
類名::函數(shù)調(diào)?
類對象.函數(shù)調(diào)?
三、靜態(tài)成員函數(shù)與普通成員函數(shù)的區(qū)別?
????????? 靜態(tài)成員函數(shù)不包含指向具體對象的指針
????????? 普通成員函數(shù)包含一個指向具體對象的指針
使用案例1:
#if 1 #include<iostream> using namespace std;//聲明類 只是知道它有哪些數(shù)據(jù)成員 并沒有為數(shù)據(jù)成員開辟空間 //只有在初始化時 才給數(shù)據(jù)成員開辟空間 class AA { public:AA(int a, int b) {m_a = a;m_b = b;}static int m_c; //在全局區(qū)申請/開辟空間,只能在類外初始化int get_c() {m_c++;return m_c;}void change_m_d(int d) {m_d = d;}static int get_d() { //加static的目的是為了讓類共享m_d++;//cout << m_a << endl; 編譯錯誤 靜態(tài)成員函數(shù)只能通過對象調(diào)用非靜態(tài)數(shù)據(jù)成員 不能直接調(diào)用return m_d;} private:static int m_d;int m_a;int m_b; }; //靜態(tài)成員變量的初始化 只能在類外 int AA ::m_c= 10; int AA::m_d = 20;void test01() {static int a = 10; //只會初始化一次a++;cout << a << endl; } void test02() {test01(); //11test01(); //12//cout << a << endl; 編譯錯誤 a并不是全局變量,所以這個沒有a的定義 } //此時說明此文件的test03函數(shù)只能在此文件使用 其他文件不可訪問 static void test03() {} void test04() {AA a1(10, 20);AA a2(100, 200);cout << a1.get_c() << endl; //11cout << a2.get_c() << endl; //12cout << a1.m_c << endl; //12AA::m_c = 100;cout << a1.get_c() << endl; //101cout << AA::m_c << endl; //101 } void test05() {AA a1(10, 20);AA a2(100, 200);cout << a1.get_d() << endl; //21cout << a2.get_d() << endl; //22//AA::change_m_d(100); 編譯錯誤 此種方式只適用于靜態(tài)數(shù)據(jù)成員 或者靜態(tài)成員函數(shù)a1.change_m_d(100);AA::get_d();cout << a1.get_d() << endl; //102cout << a2.get_d() << endl; //103 } int main() {//test02();//test04();test05();return 0; } #endif使用案例2:
#if 1 #include<iostream> using namespace std; class Student { public:Student(int id, int score) {m_id = id;m_score = score;m_num++;m_sum_score+=score;}static int get_m_num() {return m_num;}static double get_score_average() {return m_sum_score/m_num;} private:int m_id;int m_score;static int m_num;static double m_sum_score; }; int Student::m_num = 0; double Student::m_sum_score = 0;int main() {Student s1(1, 70);Student s2(2, 80);Student s3(3, 90);//cout << "人數(shù)和:" << Student::m_num << endl;m_num為私有成員 不可在類外訪問cout << "人數(shù)和:" << Student::get_m_num() << endl;cout << "平均分:" << Student::get_score_average() << endl; } #endifstatic占用內(nèi)存使用案例:
#if 1 #include <iostream> using namespace std; class C1 { public:int i; //4int j; //4int k; //4 }; //12 class C2 { public:int i;int j;int k;static int m; //4 public:int getK() const { return k; } //4void setK(int val) { k = val; } //4 }; struct S1 {int i;int j;int k; }; //12 struct S2 {int i;int j;int k;static int m; }; //12? int main() {cout << "c1 : " << sizeof(C1) << endl; //12cout << "c1 : " << sizeof(C2) << endl; //12cout << "c1 : " << sizeof(S1) << endl; //12cout << "c1 : " << sizeof(S2) << endl; //12return 0; }#endif參考自 https://www.cnblogs.com/Tempt/p/9987510.html
總結(jié)
以上是生活随笔為你收集整理的C++基础07-类之静态成员变量和成员函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++中log的底数理解
- 下一篇: C++基础13-类和对象之继承1