成员变量是数组,通过初始化列表来初始化

需求 今天编译是报错: warning: list-initializer for non-class type must not be parenthesized, 报错的地方是对类中包含的成员变量是一个数组,然后使用了列表初始化。 解决 其实这个报错提示已经说的很清楚了,对非类使用了列表初始化并且加了括号。所以解决的方法也很简单,这种数组成员变量初始化使用列表初始化时,不要加括号就 ok 了。 class Sn74cbtlv3253 { //using FuncVoidVoid = void (*)(void); using FuncVoidBool = void (*)(bool); public: enum Index { kIndex1 = 0, kIndex2, kIndex3, kIndex4, }; public: Sn74cbtlv3253(FuncVoidBool s0, FuncVoidBool s1, FuncVoidBool oe) : s0_(s0), s1_(s1), oe_(oe) {}; void Switch(Index index); void Disconnect(); private: FuncVoidBool s0_; FuncVoidBool s1_; FuncVoidBool oe_; }; class Channel { public: enum ChannelIndex { kChannel1 = 0, kChannel2, kChannel3, kChannel4, kChannel5, kChannel6, kChannel7, kChannel8, kChannel9, kChannel10, kChannel11, kChannel12, }; public: Channel(); void Switch(ChannelIndex index); void Disconnect(); private: Sn74cbtlv3253 sn74cbtlv3253_[6][2]; }; Channel::Channel() : sn74cbtlv3253_{ {{Chip1S0PinControl, Chip1S1PinControl, Chip1En1PinControl}, {Chip1S0PinControl, Chip1S1PinControl, Chip1En2PinControl}}, {{Chip2S0PinControl, Chip2S1PinControl, Chip2En1PinControl}, {Chip2S0PinControl, Chip2S1PinControl, Chip2En2PinControl}}, {{Chip3S0PinControl, Chip3S1PinControl, Chip3En1PinControl}, {Chip3S0PinControl, Chip3S1PinControl, Chip3En2PinControl}}, {{Chip4S0PinControl, Chip4S1PinControl, Chip4En1PinControl}, {Chip4S0PinControl, Chip4S1PinControl, Chip4En2PinControl}}, {{Chip5S0PinControl, Chip5S1PinControl, Chip5En1PinControl}, {Chip5S0PinControl, Chip5S1PinControl, Chip5En2PinControl}}, {{Chip6S0PinControl, Chip6S1PinControl, Chip6En1PinControl}, {Chip6S0PinControl, Chip6S1PinControl, Chip6En2PinControl}}, } { Disconnect(); } 参考 list initializer for non class type must not be parenthesized werror...

<span title='2023-05-19 10:30:00 +0800 CST'>2023-05-19</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;155 words&nbsp;·&nbsp;RamLife

成员变量是类的对象,通过初始化列表来初始化

需求 类中包含的成员变量是另外一个类的对象,如何对这种成员变量进行初始化? 解决 这种类对象成员变量初始化可以使用初始化列表。 class Hc595 { using FuncVoidVoid = void (*)(void); using FuncVoidBool = void (*)(bool); public: Hc595(FuncVoidVoid sck, FuncVoidVoid rck, FuncVoidBool bit, FuncVoidBool oe, FuncVoidVoid bit_delay) : sck_(sck), rck_(rck), bit_(bit), oe_(oe), bit_delay_(bit_delay) {}; void ShiftByte(uint8_t b); void Store(); void Display(bool en); private: FuncVoidVoid sck_; FuncVoidVoid rck_; FuncVoidBool bit_; FuncVoidBool oe_; FuncVoidVoid bit_delay_; }; class Led { public: enum LedIndex { kLed1 = 0, kLed2, kLed3, kLed4, kLed5, kLed6, kLed7, kLed8, kLed9, kLed10, kLed11, kLed12, }; public: Led(); void Set(LedIndex index, bool g, bool r, bool b); void Refresh(); void ClearData(); private: static const uint8_t kDataLength = 12; static const uint8_t kBufLength = 5; std::array<uint8_t, kDataLength> data_ {}; std::array<uint8_t, kBufLength> buf_ {}; Hc595 hc_; }; Led::Led() : hc_({SckRiseEdge, RckRiseEdge, DataBitPinControl, nullptr, nullptr}) { //hc_ = {SckRiseEdge, RckRiseEdge, DataBitPinControl, nullptr, nullptr}; hc_....

<span title='2023-05-18 10:30:00 +0800 CST'>2023-05-18</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;138 words&nbsp;·&nbsp;RamLife

成员变量是引用,需要通过初始化列表来初始化

需求 类中包含的成员变量是引用,如何对这种引用成员变量进行初始化? 解决 这种引用成员变量初始化只能使用初始化列表。 class CommandProcess { public: CommandProcess(uint64_t& send_count); private: uint64_t& msg_send_count_; }; CommandProcess::CommandProcess(uint64_t& send_count) : msg_send_count_(send_count) { } 参考 C/C++ - 类中成员变量是引用

<span title='2023-05-10 14:51:00 +0800 CST'>2023-05-10</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;24 words&nbsp;·&nbsp;RamLife

c++ struct 使用初始化列表无效

需求 今天使用统一初始化, {} 初始化 struct 之后,发现数据还是 0,没有初始化进去。 解决 经过网上查找之后,找到问题所在,列表初始化只能用于 aggregate, 而 class, struct, union 是否属于 aggregate, 需要满足以下几个条件: 无自定义构造函数 无私有或受到保护的非静态数据成员 无基类 无虚函数 类的定义中,不能有被 {} 和 = 直接初始化的非静态数据成员 // 自定义构造函数 struct Test { int x; int y; Test(int, int) {} }; // 保护非静态数据成员 struct Test { int x; int y; protected: int z; }; // 基类 struct base{}; struct Test : base { int x; int y; }; // 虚函数 struct Test { int x; int y; virtual void func(int, int) {} }; // 直接初始化 struct Test { int x; int y = 5; }; 所以只要注意以上几点即可,但是如果一定要这种情况下还要能使用列表进行初始化,也是可以的,需要如下定义:...

<span title='2023-04-27 11:45:00 +0800 CST'>2023-04-27</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;116 words&nbsp;·&nbsp;RamLife