Proper way to initialize C++ structs

后端 未结 6 528
误落风尘
误落风尘 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条回答
  •  轮回少年
    2020-12-07 08:55

    That seems to me the easiest way. Structure members can be initialized using curly braces ‘{}’. For example, following is a valid initialization.

    struct Point 
    { 
       int x, y; 
    };  
    
    int main() 
    { 
       // A valid initialization. member x gets value 0 and y 
       // gets value 1.  The order of declaration is followed. 
       struct Point p1 = {0, 1};  
    }
    

    There is good information about structs in c++ - https://www.geeksforgeeks.org/structures-in-cpp/

提交回复
热议问题