需求
类中包含的成员变量是另外一个类的对象,如何对这种成员变量进行初始化?
解决
这种类对象成员变量初始化可以使用初始化列表。
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_.Display(true);
}