全域變數破壞封裝、汙染命名空間、難以追蹤修改來源。static 成員提供「類別級別」的共享狀態,兼具:
private,僅供成員函數存取。// Test.h
class Test {
private:
static int count; // 僅宣告,不分配記憶體
public:
static int getCount();
void increment();
};
// Test.cpp
#include "Test.h"
int Test::count = 0; // 定義並初始化(必須在類別外部,且僅一次)
int Test::getCount() { return count; }
void Test::increment() { count++; }
關鍵規則:
static int count = 0; 錯誤),除非是 const static 整數型別 或 C++17 inline static。static 關鍵字。static 成員函數無 this 指標,只能存取 static 資料成員或呼叫 static 成員函數。static 成員函數可存取所有成員(含 static)。類別名::成員 或 物件.成員 / 指標->成員 存取(推薦前者,語意更清晰)。// Counter.h
#ifndef COUNTER_H
#define COUNTER_H
class Counter {
private:
static int total_; // 類別級計數器
int id_; // 實例級 ID
public:
Counter(); // 建構時自動登記
~Counter(); // 解構時自動登出
static int getTotal(); // 查詢當前存活實例數
int getId() const { return id_; }
};
#endif
// Counter.cpp
#include "Counter.h"
#include <iostream>
int Counter::total_ = 0; // 定義靜態成員
Counter::Counter() : id_(++total_) {
std::cout << "Counter #" << id_ << " created. Total: " << total_ << '\n';
}
Counter::~Counter() {
std::cout << "Counter #" << id_ << " destroyed. Remaining: " << --total_ << '\n';
}
int Counter::getTotal() { return total_; }
// main.cpp
#include "Counter.h"
int main() {
std::cout << "Initial total: " << Counter::getTotal() << '\n'; // 0
{
Counter a, b;
std::cout << "In block total: " << Counter::getTotal() << '\n'; // 2
} // a, b 离开作用域,自动析构
std::cout << "Final total: " << Counter::getTotal() << '\n'; // 0
return 0;
}
inline static:僅頭文件定義class Config {
public:
inline static int max_connections = 100; // 定義即初始化,無需 cpp 檔
};
解決「純頭文件庫」靜態成員定義難題,適用於模板、Header-only 庫。
static 成員本身不提供執行緒同步。多執行緒環境需搭配 std::mutex、std::atomic 或無鎖資料結構。
class Singleton {
private:
Singleton() = default;
static Singleton instance_; // 唯一實例
public:
static Singleton& get() { return instance_; }
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
};
Singleton Singleton::instance_; // 定義
static 成員是「受控的全域狀態」:以類別為邊界封裝、以靜態儲存區保證唯一性、以存取控制權限保護不變量。善用它,能大幅減少全域變數濫用,提升代碼模組化與可維護性。