C++ 中的结构和类
在 C++ 中,如果您创建这样的结构或类,
struct foo {
}
您必须像在 C 中那样使用结构或类限定符来创建另一个结构或类,如果不是,那是为什么呢?什么时候适合在 C++ 中使用它
示例
struct foo a;
谢谢 :-)
In c++ if you create a struct or class like this
struct foo {
}
Must you use the struct or class qualifier to create another one as you do in c, if not why is that? When would it be appropriate to use it in C++
Example
struct foo a;
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
struct
不是必需的。在 C++ 中,struct
和class
之间的唯一区别是,在struct
中,成员是public
默认情况下,在类
中,它们默认是私有
。The
struct
is not required. In C++, the only difference between astruct
and aclass
is that in astruct
, members arepublic
by default, while in aclass
, they'reprivate
by default.不,在 C++ 中实例化时不需要
struct
关键字。顺便说一下,结构体定义应该以;
结尾No,
struct
keyword is not required while instantiating in C++. By the way, struct definition should end with a;
你可以,但你不必这样做。最好不要,如果您不打算将代码与纯 C 混合,那么这才是正确的 C++ 风格。原因是 C++ 的设计者认为 C 的标签概念过于复杂。我倾向于同意。
You can, but you don't have to. And you better not, it's the proper C++ style if you don't intend to mix your code with pure C. The reason is that the designers of C++ thought that C's concept of tags was unnecessarily complicated. I tend to agree.
在 C 中,您可以通过使用 typedef 来避免它。在 C++ 中,类型是自动创建的,因此无需显式声明。
C
语法使其类似:然后您可以通过说
foo x;
使用它In C you can avoid it by using a typedef. In C++ the type is created automatically hence no need to explicitly state it.
The
C
syntax to make it similar:Then you can use it by saying
foo x;