c++ 类内初始化非常量静态成员

需求 在类内直接初始化非常量静态成员,编译时给了报错: error: ISO C++ forbids in-class initialization of non-const static member 解决 解决也非常简单,使用 C++17, 然后增加 inline 标识,就可以了。 class A { virtual void fun1(){}; int a; public: static inline int b=0; } 参考 C++:错误:ISO C++ forbids in-class initialization of non-const static member ‘A::b’

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

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

需求 今天编译是报错: 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++ 类中初始化另外一个类只可以用 {} 和 =

需求 在类中初始化另外一个类使用什么符号呢? () 可以吗? 解决 在类中给成员变量初始化只能用 {} 和 = ,不可以使用 (). 一般推荐 {} 这样的统一初始化形式。 参考 为何在类中初始化另一个类会出错? Non-static data members 构造函数与成员初始化器列表

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