Explicit initialization of struct/class members

前端 未结 4 1972
清歌不尽
清歌不尽 2021-02-13 15:22
struct some_struct{
    int a;
};
some_struct n = {};

n.a will be 0 after this;

I know this braces form of initialization is inherited from C a

4条回答
  •  太阳男子
    2021-02-13 16:18

    The empty braces form of initialization is standard in C++ (it's permitted explicitly by the grammar). See C Static Array Initialization - how verbose do I need to be? for more details if you're interested.

    I assume that it was added to C++ because it might not be appropriate for a 0 value to be used for a default init value in all situations.

提交回复
热议问题