简体   繁体   中英

Access struct elements in a class linitalizer list

I'm working on an embedded-system project using EFM32GG11 series micro-controller. In project I've to read data from multiple serial port, process the data and forward the data to server via Ethernet.

I've created a class that will handle serial port. Multiple object of this class will be created.

I've created a constructor with initialize list. I've a question: Is their a way to directly access structure member in initialization list? - uart_init.baudRate(baud_rate)

class SerialPort {
public :
    enum PortList{
        COM1,   //RS-232
        COM2    //RS-232 -- 8 more ports
    };
private:
    PortList                                    port_no;
    UARTDRV_Init_t                              uart_init;
    uint32_t                                    baud_rate;
    char                                        parity;
    uint8_t                                     stop_bit;
    bool                                        single_line_mode;
    uint16_t                                    block_time; //in milli-seconds
public:
    SerialPort(PortList port_no, uint16_t baud_rate, char parity, uint8_t stop_bit,
        bool single_line_mode, uint16_t block_time) : port_no(port_no), uart_init.baudRate(baud_rate), parity(parity), stop_bit(stop_bit), single_line_mode(single_line_mode),
                block_time(block_time)
    {
          //Further processing post initialization
    }


};

UARTDRV_Init_t Strurcture:

typedef struct {
  USART_TypeDef              *port;             ///< The peripheral used for UART
  uint32_t                   baudRate;          ///< UART baud rate
} UARTDRV_InitUart_t;

You can use designated initializers (since C++20) to specify the member to be initialized. Eg

SerialPort(PortList port_no, uint16_t baud_rate, char parity, uint8_t stop_bit,
    bool single_line_mode, uint16_t block_time) : port_no(port_no), uart_init {.baudRate=baud_rate}, parity(parity), stop_bit(stop_bit), single_line_mode(single_line_mode),
//                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            block_time(block_time)
{
      //Further processing post initialization
}

Before C++17, we can only initialize the data member itself in member initializer list and can't specify its subobject further more.

If you know the what the default for port should be, you could do it this way, assuming port is initialized to nullptr or something:

SerialPort(PortList port_no, uint16_t baud_rate, char parity, uint8_t stop_bit,
    bool single_line_mode, uint16_t block_time) : port_no(port_no), uart_init {nullptr, baud_rate}, parity(parity), stop_bit(stop_bit), single_line_mode(single_line_mode),
            block_time(block_time)
{
      ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM