C++ static静态成员函数详解
在類中,static 除了可以聲明靜態(tài)成員變量,還可以聲明靜態(tài)成員函數(shù)。普通成員函數(shù)可以訪問所有成員(包括成員變量和成員函數(shù)),靜態(tài)成員函數(shù)只能訪問靜態(tài)成員。
編譯器在編譯一個普通成員函數(shù)時,會隱式地增加一個形參 this,并把當前對象的地址賦值給 this,所以普通成員函數(shù)只能在創(chuàng)建對象后通過對象來調用,因為它需要當前對象的地址。而靜態(tài)成員函數(shù)可以通過類來直接調用,編譯器不會為它增加形參 this,它不需要當前對象的地址,所以不管有沒有創(chuàng)建對象,都可以調用靜態(tài)成員函數(shù)。
普通成員變量占用對象的內存,靜態(tài)成員函數(shù)沒有 this 指針,不知道指向哪個對象,無法訪問對象的成員變量,也就是說靜態(tài)成員函數(shù)不能訪問普通成員變量,只能訪問靜態(tài)成員變量。
普通成員函數(shù)必須通過對象才能調用,而靜態(tài)成員函數(shù)沒有 this 指針,無法在函數(shù)體內部訪問某個對象,所以不能調用普通成員函數(shù),只能調用靜態(tài)成員函數(shù)。
靜態(tài)成員函數(shù)與普通成員函數(shù)的根本區(qū)別在于:普通成員函數(shù)有 this 指針,可以訪問類中的任意成員;而靜態(tài)成員函數(shù)沒有 this 指針,只能訪問靜態(tài)成員(包括靜態(tài)成員變量和靜態(tài)成員函數(shù))。
下面是一個完整的例子,該例通過靜態(tài)成員函數(shù)來獲得學生的總人數(shù)和總成績:
#include <iostream> using namespace std; class Student{ public:Student(char *name, int age, float score);//構造函數(shù)void show();//聲明靜態(tài)成員函數(shù)static int getTotal();static float getPoints(); private:static int m_total; //總人數(shù)static float m_points; //總成績char *m_name;int m_age;float m_score; }; int Student::m_total = 0; float Student::m_points = 0.0; //C++構造函數(shù)初始化列表 //http://c.biancheng.net/view/2223.html 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; } //定義靜態(tài)成員函數(shù) 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
總人數(shù) m_total 和總成績 m_points 由各個對象累加得到,必須聲明為 static 才能共享;getTotal()、getPoints() 分別用來獲取總人數(shù)和總成績,為了訪問 static 成員變量,我們將這兩個函數(shù)也聲明為 static。
在C++中,靜態(tài)成員函數(shù)的主要目的是訪問靜態(tài)成員。getTotal()、getPoints() 當然也可以聲明為普通成員函數(shù),但是它們都只對靜態(tài)成員進行操作,加上 static 語義更加明確。
和靜態(tài)成員變量類似,靜態(tài)成員函數(shù)在聲明時要加 static,在定義時不能加 static。靜態(tài)成員函數(shù)可以通過類來調用(一般都是這樣做),也可以通過對象來調用,上例僅僅演示了如何通過類來調用。
總結
以上是生活随笔為你收集整理的C++ static静态成员函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ static静态成员变量详解
- 下一篇: C++友元函数和友元类(一)