我有一个正在序列化以通过 unix 套接字发送的类,它必须有一个字符串,我已将其存储为 char 数组。我可以在构造函数中以不同于我在这里完成的方式初始化它吗?
typedef struct SerialFunctionStatus_t {
SerialFunctionStatus_t()
: serial_rx_count(0), serial_tx_count(0), socket_rx_count(0), socket_tx_count(0)
{ port[0] = '\0'; }
uint32_t serial_rx_count;
uint32_t serial_tx_count;
uint32_t socket_rx_count;
uint32_t socket_tx_count;
char port[20];
} SerialFunctionStatus_t;
最佳答案
将 port()
放入初始化列表。这导致端口被“值初始化”(12.6.2),对于内置数组意味着零初始化(8.5)。
关于c++ - 如何在构造函数中初始化 char 数组样式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/245619/