Is there a way to make a C++ struct value-initialize all POD member variables?

后端 未结 3 1325
陌清茗
陌清茗 2020-12-01 20:01

Suppose I have a C++ struct that has both POD and non-POD member variables:

struct Struct {
    std::string String;
    int Int;
};

and in

3条回答
  •  一整个雨季
    2020-12-01 20:17

    Linked Question here

    Is there a way to enforce value-initialization of all POD member variables without explicitly adding their initialization in this case?

    I am not sure whether something like that is possible [directly] or not but the following works

    prasoon@prasoon-desktop ~ $ cat check.cpp && clang++ check.cpp && ./a.out
    #include 
    struct Struct {
        std::string String;
        int Int;
        bool k;
        // add add add
    };
    
    struct InStruct:Struct
    {
       InStruct():Struct(){}
    };
    
    int main()
    {
       InStruct i;
       std::cout<< i.k << "  " << i.Int << std::endl; 
    }
    0  0
    prasoon@prasoon-desktop ~ $ 
    

提交回复
热议问题