static 修飾的類數據成員不屬於任何對象,屬於類本身。全程序僅有一份實例,存儲在靜態存儲區,所有對象(含派生類對象)共享。
// MyClass.h
class MyClass {
static const int MAX_SIZE = 100; // C++11: 整型靜態常量可類內初始化 (作為編譯期常量)
static int count; // 僅聲明!無內存分配
static std::string name; // 非整型/非常量 必須類外定義
};
// MyClass.cpp (僅此文件定義一次)
int MyClass::count = 0; // 定義並初始化
std::string MyClass::name = "Default"; // 定義並初始化
// 定義時不加 static,不受 private/protected 限制
原因:頭文件多處包含 → 多次定義鏈接錯誤。C++17 inline static int count = 0; 可在類內定義,解決此問題。
class Base { public: static int s_val; };
int Base::s_val = 0;
class Derived : public Base {};
int main() {
Base b; Derived d;
b.s_val = 1;
std::cout << d.s_val; // 1!派生類對象訪問基類靜態成員 = 同一內存
d.s_val = 2;
std::cout << b.s_val; // 2!
}
class Config {
static int default_port; // 靜態成員
int port; // 普通成員
public:
// 合法:靜態成員有固定地址,編譯期可知
void setPort(int p = default_port) { port = p; }
// 非法:普通成員無 this 無法確定值
// void setPort2(int p = port); // 編譯錯誤
};
int Config::default_port = 8080;
class Node {
static Node sentinel; // 合法:靜態成員,單例模式常用
Node* next; // 合法:指針/引用
// Node child; // 非法:無限遞歸大小
};
Node Node::sentinel; // 定義
class Logger {
mutable int mutable_cnt = 0; // mutable 也可
static int static_cnt = 0; // 靜態也可
int normal_cnt = 0;
public:
void log() const {
mutable_cnt++; // OK
static_cnt++; // OK!靜態成員不屬於對象,不受 const 約束
// normal_cnt++; // 錯誤:const 成員函數不可修改非 mutable 普通成員
}
};
int Logger::static_cnt = 0;
本質:const 成員函數的 this 是 const T*,約束的是對象非靜態成員。靜態成員無 this,自然不受限。
public/protected/private 同樣生效。private static 成員類外不可訪問,但類內定義時不受限。
main 之前。
class ModernConfig {
public:
// C++17 inline static: 類內定義,無需 .cpp,解決初始化順序、多定義問題
inline static int max_connections = 1000;
inline static std::string server_name = "DefaultServer";
// C++20: 可用 constinit 保證靜態初始化 (編譯期完成)
constinit static int protocol_version = 2;
// 私有靜態 + 靜態工廠方法 (單例/受控訪問)
private:
inline static std::unique_ptr instance;
public:
static ModernConfig& getInstance() {
if (!instance) instance = std::make_unique();
return *instance;
}
};
靜態數據成員是類級共享狀態的標準實現。牢記:聲明在類內,定義在類外 (C++17 前);共享於基類/派生類;可作默認參數、類類型成員;const 成員函數可修改。現代 C++ 用 inline static 徹底簡化定義管理,配合 constinit 保證初始化安全。