Tho*_*ing 7 c++ static initialization stateless
假设我有一个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
};
Run Code Online (Sandbox Code Playgroud)
// 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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1188 次 |
最近记录: |