在C ++中初始化结构体的语法是什么? [英] What is the syntax for initializing a struct in C++?

查看:91
本文介绍了在C ++中初始化结构体的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过这种方式构建一个构造函数来初始化一个结构:

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

mystruct struct1(a,b); 

与初始化类相同?

或者我必须使用这种方式:

Or do I have to use this way:

mystruct struct1=mystruct(a,b);  

推荐答案

您可以使用与 class 相同的语法。在C ++中,除了 public 对于 struct 和<$ c的默认访问说明符之间没有区别$ c> private 为 class 。有关详细说明,请参阅此处: struct和类之间的区别

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

这篇关于在C ++中初始化结构体的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆