c++學習筆記之類和對象(三、static靜態成員變量和靜態成員函數)


一、static靜態成員變量

對象的內存中包含了成員變量,不同的對象占用不同的內存,這使得不同對象的成員變量相互獨立,它們的值不受其他對象的影響。是有時候我們希望在多個對象之間共享數據,對象 a 改變了某份數據后對象 b 可以檢測到。共享數據的典型使用場景是計數。

在C++中,我們可以使用靜態成員變量來實現多個對象共享數據的目標。靜態成員變量是一種特殊的成員變量,它被關鍵字static修飾。static 成員變量屬於類,不屬於某個具體的對象,即使創建多個對象,也只為其分配一份內存,所有對象使用的都是這份內存中的數據。當某個對象修改了靜態成員變量,也會影響到其他對象。

#include <iostream>
using namespace std;

class Student{
public:
    Student(char *name, int age, float score);
    void show();
private:
    static int m_total;  //靜態成員變量
private:
    char *m_name;
    int m_age;
    float m_score;
};

//初始化靜態成員變量
int Student::m_total = 0;

Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){
    m_total++;  //操作靜態成員變量
}
void Student::show(){
    cout<<m_name<<"的年齡是"<<m_age<<",成績是"<<m_score<<"(當前共有"<<m_total<<"名學生)"<<endl;
}

int main(){
    //創建匿名對象
    (new Student("小明", 15, 90)) -> show();
    (new Student("李磊", 16, 80)) -> show();
    (new Student("張華", 16, 99)) -> show();
    (new Student("王康", 14, 60)) -> show();

    return 0;
}

運行結果:

小明的年齡是15,成績是90(當前共有1名學生)

李磊的年齡是16,成績是80(當前共有2名學生)

張華的年齡是16,成績是99(當前共有3名學生)

王康的年齡是14,成績是60(當前共有4名學生)

 

2、在類中,static 除了可以聲明靜態成員變量,還可以聲明靜態成員函數。普通成員函數可以訪問所有成員(包括成員變量和成員函數),靜態成員函數只能訪問靜態成員。

編譯器在編譯一個普通成員函數時,會隱式地增加一個形參 this,並把當前對象的地址賦值給 this,所以普通成員函數只能在創建對象后通過對象來調用,因為它需要當前對象的地址。而靜態成員函數可以通過類來直接調用,編譯器不會為它增加形參 this,它不需要當前對象的地址,所以不管有沒有創建對象,都可以調用靜態成員函數。

普通成員變量占用對象的內存,靜態成員函數沒有 this 指針,不知道指向哪個對象,無法訪問對象的成員變量,也就是說靜態成員函數不能訪問普通成員變量,只能訪問靜態成員變量。

普通成員函數必須通過對象才能調用,而靜態成員函數沒有 this 指針,無法在函數體內部訪問某個對象,所以不能調用普通成員函數,只能調用靜態成員函數。

靜態成員函數與普通成員函數的根本區別在於:普通成員函數有 this 指針,可以訪問類中的任意成員;而靜態成員函數沒有 this 指針,只能訪問靜態成員(包括靜態成員變量和靜態成員函數)。

#include <iostream>
using namespace std;

class Student{
public:
    Student(char *name, int age, float score);
    void show();
public:  //聲明靜態成員函數
    static int getTotal();
    static float getPoints();
private:
    static int m_total;  //總人數
    static float m_points;  //總成績
private:
    char *m_name;
    int m_age;
    float m_score;
};

int Student::m_total = 0;
float Student::m_points = 0.0;

Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){
    m_total++;
    m_points += score;
}
void Student::show(){
    cout<<m_name<<"的年齡是"<<m_age<<",成績是"<<m_score<<endl;
}
//定義靜態成員函數
int Student::getTotal(){
    return m_total;
}
float Student::getPoints(){
    return m_points;
}

int main(){
    (new Student("小明", 15, 90.6)) -> show();
    (new Student("李磊", 16, 80.5)) -> show();
    (new Student("張華", 16, 99.0)) -> show();
    (new Student("王康", 14, 60.8)) -> show();

    int total = Student::getTotal();
    float points = Student::getPoints();
    cout<<"當前共有"<<total<<"名學生,總成績是"<<points<<",平均分是"<<points/total<<endl;

    return 0;
}

運行結果:

小明的年齡是15,成績是90.6

李磊的年齡是16,成績是80.5

張華的年齡是16,成績是99

王康的年齡是14,成績是60.8

當前共有4名學生,總成績是330.9,平均分是82.725

[Finished in 1.8s]

 

注:內容純屬於個人學習筆記,記錄學習而己。

——


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM