1. 程式人生 > >C++ 類中的static關鍵字

C++ 類中的static關鍵字

1.靜態資料成員

我們可以使用 static 關鍵字來把類成員定義為靜態的。當我們宣告類的成員為靜態時,這意味著無論建立多少個類的物件,靜態成員都只有一個副本。

靜態成員在類的所有物件中是共享的。如果不存在其他的初始化語句,在建立第一個物件時,所有的靜態資料都會被初始化為零。我們不能把靜態成員的初始化放置在類的定義中,但是可以在類的外部通過使用範圍解析運算子 :: 來重新宣告靜態變數從而對它進行初始化,如下面的例項所示。

下面的例項有助於更好地理解靜態成員資料的概念:


#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      static int objectCount;
      // 建構函式定義
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次建立物件時增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // 長度
      double breadth;    // 寬度
      double height;     // 高度
};
 
// 初始化類 Box 的靜態成員
int Box::objectCount = 0;
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // 宣告 box1
   Box Box2(8.5, 6.0, 2.0);    // 宣告 box2
 
   // 輸出物件的總數
   cout << "Total objects: " << Box::objectCount << endl;
 
   return 0;
}

當上面的程式碼被編譯和執行時,它會產生下列結果:

Constructor called.
Constructor called.
Total objects: 2

2.靜態成員函式

如果把函式成員宣告為靜態的,就可以把函式與類的任何特定物件獨立開來。靜態成員函式即使在類物件不存在的情況下也能被呼叫,靜態函式只要使用類名加範圍解析運算子 :: 就可以訪問。

靜態成員函式只能訪問靜態成員資料、其他靜態成員函式和類外部的其他函式。

靜態成員函式有一個類範圍,他們不能訪問類的 this 指標。您可以使用靜態成員函式來判斷類的某些物件是否已被建立。

靜態成員函式與普通成員函式的區別:

  • 靜態成員函式沒有 this 指標,只能訪問靜態成員(包括靜態成員變數和靜態成員函式)。
  • 普通成員函式有 this 指標,可以訪問類中的任意成員;而靜態成員函式沒有 this 指標。

下面的例項有助於更好地理解靜態成員函式的概念:


#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      static int objectCount;
      // 建構函式定義
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次建立物件時增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      static int getCount()
      {
         return objectCount;
      }
   private:
      double length;     // 長度
      double breadth;    // 寬度
      double height;     // 高度
};
 
// 初始化類 Box 的靜態成員
int Box::objectCount = 0;
 
int main(void)
{
  
   // 在建立物件之前輸出物件的總數
   cout << "Inital Stage Count: " << Box::getCount() << endl;
 
   Box Box1(3.3, 1.2, 1.5);    // 宣告 box1
   Box Box2(8.5, 6.0, 2.0);    // 宣告 box2
 
   // 在建立物件之後輸出物件的總數
   cout << "Final Stage Count: " << Box::getCount() << endl;
 
   return 0;
}

當上面的程式碼被編譯和執行時,它會產生下列結果:

Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

 

靜態成員變數在類中僅僅是宣告,沒有定義,所以要在類的外面定義,實際上是給靜態成員變數分配記憶體。如果不加定義就會報錯,初始化是賦一個初始值,而定義是分配記憶體。

#include <iostream>
 
using namespace std;

class Box
{
   public:
      static int objectCount;
      // 建構函式定義
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次建立物件時增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // 長度
      double breadth;    // 寬度
      double height;     // 高度
};

// 初始化類 Box 的靜態成員   其實是定義並初始化的過程
int Box::objectCount = 0;
//也可這樣 定義卻不初始化
//int Box::objectCount;
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // 宣告 box1
   Box Box2(8.5, 6.0, 2.0);    // 宣告 box2

   // 輸出物件的總數
   cout << "Total objects: " << Box::objectCount << endl;

   return 0;
}

執行結果:

Constructor called.
Constructor called.
Total objects: 2