如何在构造函数中初始化以下结构的值为定义好的值?
我的代码示例中显示的两个选项似乎都有点丑陋...
struct T_AnlagenInfo01
{
// Option A
T_AnlagenInfo01() : fReserve80_0(0), fReserve80_1(0),.... {};
// Option B
T_AnlagenInfo01()
{
memset(this, 0, sizeof(T_AnlagenInfo01));
}
unsigned long fReserve80_0 : 1;
unsigned long fReserve80_1 : 1;
unsigned long fReserve80_2 : 1;
unsigned long fReserve80_3 : 1;
unsigned long fReserve80_4 : 1;
unsigned long fReserve80_5 : 1;
unsigned long fReserve80_6 : 1;
unsigned long fReserve80_7 : 1;
unsigned long fReserve81_0 : 1; // 81
unsigned long fReserve81_1 : 1;
unsigned long fReserve81_2 : 1;
unsigned long fReserve81_3 : 1;
unsigned long fReserve81_4 : 1;
unsigned long fReserve81_5 : 1;
unsigned long fReserve81_6 : 1;
unsigned long fReserve81_7 : 1;
};
memset
的替代方法,因为它也会覆盖虚表。 - Some programmer dudefReserve80_0(0)
是标准方式,实际上比memset更清晰易懂,也更易于维护和类型安全。 - Sebastian Mach