What is the syntax for initializing a struct in C++?

徘徊边缘 提交于 2019-12-11 10:48:45

问题


Can I build a constructor to initialize a struct this way:

mystruct struct1(a,b); 

the same way I initialize a class?

Or do I have to use this way:

mystruct struct1=mystruct(a,b);  

?


回答1:


You can use the same syntax as you use for class. In C++, there is no difference between them except for the default access specifiers which is public for struct and private for class. See here for detailed explaination: Difference between struct and class




回答2:


In C++ there is no difference between a structure and a class except that the data members by default are public in case of struct and private in case of class.

Furthermore there are two common modes of initialization of objects in C++

1) Direct Initialization 2) Copy Initialization




回答3:


Both ways are valid. you can do it either ways.



来源:https://stackoverflow.com/questions/4912313/what-is-the-syntax-for-initializing-a-struct-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!