クラス T があるとします。
- T には仮想関数がありません。
- T インスタンスには状態がありません。
- T には、それ自体の静的メンバー インスタンスがあります。
- T 自体には他の状態はありません。
C++ の静的初期化の大失敗により、プログラムが台無しになる可能性はありますか? 静的インスタンスの 1 つが使用前に初期化されていなくても、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;
}