假設的靜態初始化我有一個類T其中C++類無國籍
- T沒有虛函數。
- T實例沒有狀態。
- T有自己的靜態成員實例。
- T本身沒有其他狀態。
C++靜態初始化失敗可以毀了我的程序嗎?我不這麼認爲,因爲即使其中一個靜態實例在使用之前未被初始化,也不應該擔心,因爲T對象是無狀態的。
我很感興趣,這樣做,枚舉類類,像這樣:
// Switch.h
class Switch {
public:
static Switch const ON;
static Switch const OFF;
bool operator== (Switch const &s) const;
bool operator!= (Switch const &s) const;
private:
Switch() {}
Switch (Switch const &); // no implementation
Switch & operator= (Switch const &); // no implementation
};
// Switch.cpp
Switch const Switch::ON;
Switch const Switch::OFF;
bool Switch::operator== (Switch const &s) const {
return this == &s;
}
bool Switch::operator!= (Switch const &s) const {
return this != &s;
}
優秀的問題和一個有趣的想法! – 2011-05-16 22:06:58
這是什麼讓你通過一個簡單的枚舉(沒有額外的狀態)? – Nim 2011-05-16 22:08:46
請發表一個你如何真正打算使用Switch類的例子。 – 2011-05-16 22:10:52