Proper way to initialize C++ structs

后端 未结 6 520
误落风尘
误落风尘 2020-12-07 08:11

Our code involves a POD (Plain Old Datastructure) struct (it is a basic c++ struct that has other structs and POD variables in it that needs to get initialized in the beginn

6条回答
  •  -上瘾入骨i
    2020-12-07 09:08

    You need to initialize whatever members you have in your struct, e.g.:

    struct MyStruct {
      private:
        int someInt_;
        float someFloat_;
    
      public:
        MyStruct(): someInt_(0), someFloat_(1.0) {} // Initializer list will set appropriate values
    
    };
    

提交回复
热议问题