What is the syntax for initializing a struct in C+

2019-08-14 15:24发布

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);  

?

3条回答
一纸荒年 Trace。
2楼-- · 2019-08-14 15:27

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楼-- · 2019-08-14 15:36

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

查看更多
唯我独甜
4楼-- · 2019-08-14 15:49

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

查看更多
登录 后发表回答