C++靜態成員和非靜態成員的區別和使用
- 對象與對象之間的成員變量是相互獨立的。要想共用數據,則需要使用靜態成員和靜態方法。
- 只要在類中聲明靜態成員變量,即使不定義對象,也可以為靜態成員變量分配空間,進而可以使用靜態成員變量。(因為靜態成員變量在對象創建之前就已經被分配了內存空間)
- 靜態成員變量雖然在類中,但它並不是隨對象的建立而分配空間的,也不是隨對象的撤銷而釋放(一般的成員在對象建立時會分配空間,在對象撤銷時會釋放)。靜態成員變量是在程序編譯時分配空間,而在程序結束時釋放空間。
- 靜態成員的定義和聲明要加個關鍵static。靜態成員可以通過雙冒號來使用,即<類名>::<靜態成員名>。
- 初始化靜態成員變量要在類的外面進行。初始化的格式如下:數據類型 類名::靜態成員變量名 = 初值;
- 不能用參數初始化表,對靜態成員變量進行初始化。
- 既可以通過類名來對靜態成員變量進行引用,也可以通過對象名來對靜態成員變量進行引用。
- 普通成員函數和靜態成員函數的區別是:普通成員函數在參數傳遞時編譯器會隱藏地傳遞一個this指針.通過this指針來確定調用類產生的哪個對象;但是靜態成員函數沒有this指針,不知道應該訪問哪個對象中的數據,所以在程序中不可以用靜態成員函數訪問類中的普通變量.
下面通過幾個例子來總結靜態成員變量和靜態成員函數的使用規則。
一、通過類名調用靜態成員函數和非靜態成員函數
1 //例子一:通過類名調用靜態成員函數和非靜態成員函數 2 class Point{ 3 public: 4 void init() 5 {} 6 7 static void output() 8 {} 9 }; 10 11 void main() 12 { 13 Point::init();//此處編譯錯誤 非靜態成員函數必須和對象對應 14 Point::output(); 15 }
編譯出錯:錯誤 1 error C2352: “Point::init”: 非靜態成員函數的非法調用
結論一:不能通過類名來調用類的非靜態成員函數
二、通過類的對象調用靜態成員函數和非靜態成員函數
1 //例子二:通過類的對象調用靜態成員函數和非靜態成員函數 2 class Point{ 3 public: 4 void init() 5 { 6 } 7 8 static void output() 9 {} 10 }; 11 12 void main() 13 { 14 Point pt; 15 pt.init(); 16 pt.output(); 17 }
編譯通過。
結論二:類的對象可以使用靜態成員函數和非靜態成員函數。
三、在類的靜態成員函數中使用類的非靜態成員
1 //例子三:在類的靜態成員函數中使用類的非靜態成員 2 #include <iostream> 3 using namespace std; 4 5 class Point{ 6 public: 7 void init() 8 { 9 } 10 static void output() 11 { 12 cout << "m_x=" << m_x << endl;//此處編譯錯誤 靜態成員函數中不能引用非靜態成員 13 } 14 private: 15 int m_x; 16 }; 17 void main() 18 { 19 Point pt; 20 pt.output(); 21 }
編譯出錯:IntelliSense: 非靜態成員引用必須與特定對象相對
因為靜態成員函數屬於整個類,在類實例化對象之前就已經分配空間了,而類的非靜態成員必須在類實例化對象后才有內存空間,所以這個調用就會出錯,就好比沒有聲明一個變量卻提前使用它一樣。
結論三:靜態成員函數中不能引用非靜態成員。
四、在類的非靜態成員函數中使用類的靜態成員
1 //例子四:在類的非靜態成員函數中使用類的靜態成員 2 #include <iostream> 3 using namespace std; 4 5 class Point{ 6 public: 7 void init() 8 { 9 output(); 10 } 11 static void output() 12 { 13 } 14 private: 15 int m_x; 16 }; 17 void main() 18 { 19 Point pt; 20 pt.init(); 21 }
編譯通過。
結論四:類的非靜態成員可以調用靜態成員函數,但反之不能。
五、使用類的靜態成員變量
1 //例子五:使用類的靜態成員變量 2 #include <iostream> 3 using namespace std; 4 5 class Point{ 6 public: 7 Point() 8 { 9 m_nPointCount++; 10 } 11 ~Point() 12 { 13 m_nPointCount++; 14 } 15 static void output() 16 { 17 cout << "m_nPointCount=" << m_nPointCount << endl; 18 } 19 private: 20 static int m_nPointCount; 21 }; 22 23 void main() 24 { 25 Point pt; 26 pt.output(); 27 }
鏈接出錯:error LNK2001: 無法解析的外部符號 "private: static int Point::m_nPointCount" (?m_nPointCount@Point@@0HA)
這是因為類的成員變量在使用前必須先初始化。
改成如下代碼即可:
1 #include <iostream> 2 using namespace std; 3 4 class Point{ 5 public: 6 Point() 7 { 8 m_nPointCount++; 9 } 10 ~Point() 11 { 12 m_nPointCount++; 13 } 14 static void output() 15 { 16 cout << "m_nPointCount=" << m_nPointCount << endl; 17 } 18 private: 19 static int m_nPointCount; 20 }; 21 22 //類外初始化靜態成員變量時,不用帶static關鍵字 23 int Point::m_nPointCount = 0; 24 void main() 25 { 26 Point pt; 27 pt.output(); 28 }
運行結果:
結論五:類的靜態成員變量必須先初始化再使用。