C++无状态类的静态初始化
C++ static initialization of stateless class
>假设我有一个类 T 其中
- 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;
}
回答问题的第一部分,如果T
有一个有副作用的构造函数,那么你实际上可能会被静态初始化惨败烧毁。
我对你从命名空间或类中包装的枚举中看到的优势感兴趣:
namespace Switch {
enum Switch {
ON,
OFF
};
}
在大多数情况下,它会更容易使用(在您的实现中,您需要用户使用引用或指针,因为对象是不可复制的),它需要更少的代码(无需禁用构造函数并创建运算符)......
事实上,在即将推出的标准中,您几乎无需使用命名空间即可免费获得它:
enum Switch {
ON,
OFF
};
// bad, it allows this (as in the current standard):
Switch s = ON;
// good, it does also allow explicit qualification:
Switch s = Switch::ON;
你真的打算使用指针值来比较"状态"吗? 我同意@Drew,这是一个有趣的想法。 但是,如果我们假设这是一个仅标头的实现,我不确定标准是否保证它能够正常工作。
考虑当多个编译对象包含相同的 Switch::ON
和 Switch::OFF
定义时会发生什么。 由于这些是变量,而不是函数,因此链接器必须在它们之间任意决定。
当你运行测试时,流行的编译器说了什么:gcc 3,gcc 4,Microsoft C++ 2005,2008和2010,以及爱迪生设计小组的编译器之一,如 http://www.comeaucomputing.com/?
所述测试将包括:
// 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 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;
}
和
// main.cpp
#include "Switch.h"
extern int another_test();
int main(int argc, char*argv[])
{
another_test();
const Switch& current_state = Switch::ON;
const Switch& another_state = Switch::OFF;
if (current_state == another_state) {
return 1;
} else if (current_state != another_state) {
return 2;
}
return another_test();
}
和
// another_test.cpp
#include "Switch.h"
int another_test()
{
const Switch& current_state = Switch::ON;
const Switch& another_state = Switch::OFF;
if (current_state == another_state) {
return 4;
} else if (current_state != another_state) {
return 5;
}
return 6;
}
相关文章:
- 使用g++静态初始化带有命名标签的嵌套C++结构
- 静态初始化的反义词是什么?
- 在静态初始化期间运行代码
- 结构数组的嵌入式C++静态初始化
- 初始化与类类型相同的静态成员(静态初始化顺序问题)
- 使用 constexpr 和 std::array 进行静态初始化来替换动态初始化的 std::vector 的闰年
- MSVC 2017 在单个翻译单元中违反静态初始化顺序
- VC++6 线程安全静态初始化
- 具有必要副作用的静态初始化被优化掉了
- 未使用全球的静态初始化器
- w/ w/结构带char缓冲液的静态初始化[]
- 静态初始化的对象不能放在块中尝试
- 功能指针的静态初始化
- lambda(或函数)C++中的静态初始化
- C++静态初始化顺序:添加到映射中
- 复杂类型数组的静态初始化
- C++ - 在派生类中静态初始化受基类保护的成员变量
- 静态初始化插件工厂
- 静态初始化包含功能指针的对象的constexpr std ::数组
- 找出静态初始化是否结束