我尝试实现 C++11 功能(我已将此答案用作参考Can I call a constructor from another constructor (do constructor chaining) in C++?)。显然,我做错了,但我不明白为什么。
我在以下代码中收到了几个警告:
- 成员
_output
未在此构造函数中初始化 - 成员
_protocol_scanner
未在此构造函数中初始化 - 成员
_state
未在此构造函数中初始化 - 成员
_source
未在此构造函数中初始化
这是代码:
class UartScanner {
public:
UartScanner(periph::IStreamDevice *source, periph::IStreamDevice *output);
UartScanner(periph::IStreamDevice *source);
~UartScanner();
private:
typedef enum
{
WAITING_SYNC,
WAITING_UBLOX_MSG,
WAITING_NOVATEL_MSG
} states_t;
periph::IStreamDevice *_source;
periph::IStreamDevice *_output;
ProtocolScanner *_protocol_scanner;
states_t _state;
};
UartScanner::UartScanner(periph::IStreamDevice *source, IStreamDevice *output):
_source(source),
_output(output),
_state(WAITING_SYNC)
{
_protocol_scanner = new ProtocolScanner(source,output);
}
UartScanner::UartScanner(periph::IStreamDevice *source): UartScanner(source,0)
{
}
class IStreamDevice {
public:
virtual ~IStreamDevice() {}
virtual uint32_t read(uint8_t* data, uint32_t size) = 0;
virtual uint32_t write(const uint8_t* data, uint32_t size) = 0;
};