我对 std::bitset 的理解有问题。我想使用 bitset 作为多个功能的开关。
想象一下 A 类和函数 Func1。构造函数看起来像这样 A(bool procFunc)。用法很简单。如果我想使用 Func1,我将通过 true,否则为 false。简单的。我有多种选择,一个 bool 是不够的,所以我想使用 std::bitset。
非常非常简单的草稿:
class A {
public:
A(); // default ctor
A(bool procFunc); // parameterized ctor with bool
A(std::bitset<N>& bs); // parameterized ctor with bitset
private:
bool _procFunc;
std::bitset<4> _bs;
void Func1();
void Func2();
void Func3();
void Func4();
};
A:A() {}
A:A(bool procFunc) :
_procFunc(procFunc)
{}
A:A(std::bitset<N>& bs) :
_bs(bs)
{}
如果我只有 Func1。我会这样打开:
if(_procFunc == true){
Func1();
}
但我不知道如何准确地使用 std::bitset 进行多项选择。我想定义一些枚举或类似的东西并定义0001调用Func1()...... 0010调用Func2()...... 0100调用Func2()...... 1000调用Func4()。
谢谢你的帮助。