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

后端 未结 3 1327
陌清茗
陌清茗 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:25

    You can add a base struct:

    struct PODStruct
    {
      PODStruct(unsinged int count) { memset( this, 0, count);}
    };
    

    And then your struct derived from this base struct, first place if you have more than one base structs,

    struct Struct : PODStruct
    {
      Struct();
      std::string Str;
      int Int;
    }
    
    Struc::Struct() : PODStruct(sizeof(Struct))
    {
    }
    

提交回复
热议问题