C++ - 结构与类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4791883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - struct vs. class
提问by Simon Richter
Possible Duplicates:
C/C++ Struct vs Class
What are POD types in C++?
Hi,
你好,
In the C++ In a Nutshell book, in chapter 6: classes, unders Access specifiers, mentioned the following:
在C++ In a Nutshell book,第 6 章: classes, unders Access specifiers,提到了以下内容:
In a class definition, the default access for members and base classes is private. In a struct definition, the default is public. That is the only difference between a class and a struct, although by convention, some programmers use struct only for POD classesand use class for all other classes.
在类定义中,成员和基类的默认访问权限是私有的。在 struct 定义中,默认值为 public。这是 class 和 struct 之间的唯一区别,尽管按照惯例,一些程序员仅将 struct 用于POD 类,而将 class 用于所有其他类。
My questions here are:
我的问题是:
- Isn't there another difference between classes and structs in that structs don't hold functions and just hold data?
- What are POD classes? And, what is meant by all other classes here? Are there then special classes?
- 类和结构之间是否还有另一个区别,即结构不保存函数而只保存数据?
- 什么是 POD 类?而且,这里的所有其他类是什么意思?那么有专门的班级吗?
Thanks.
谢谢。
回答by Simon Richter
The other difference is that
另一个区别是
template<class T> ...
is allowed, but
是允许的,但是
template<struct T> ...
is not.
不是。
回答by BlueMonkMN
You could prove to yourself that there is no other difference by trying to define a function in a struct. I remember even my college professor who was teaching about structs and classes in C++ was surprised to learn this (after being corrected by a student). I believe it, though. It was kind of amusing. The professor kept saying what the differences were and the student kept saying "actually you can do that in a struct too". Finally the prof. asked "OK, what is the difference" and the student informed him that the only difference was the default accessibility of members.
您可以通过尝试在结构中定义函数来向自己证明没有其他区别。我记得甚至我的大学教授在 C++ 中教授结构和类的时候也很惊讶(在被学生纠正之后)。不过我相信。这有点有趣。教授一直在说差异是什么,而学生一直在说“实际上你也可以在结构中做到这一点”。最后是教授。问“好的,有什么区别”,学生告诉他唯一的区别是成员的默认可访问性。
A quick Google search suggests that POD stands for "Plain Old Data".
快速谷歌搜索表明 POD 代表“Plain Old Data”。
回答by Sriram
POD classes are Plain-Old data classes that have only data members and nothing else. There are a few questions on stackoverflow about the same. Find one here.
POD 类是Plain-Old 数据类,只有数据成员,没有其他任何东西。stackoverflow 上也有一些类似的问题。在这里找到一个。
Also, you can have functions as members of structs in C++ but not in C. You need to have pointers to functions as members in structs in C.
此外,您可以将函数作为 C++ 中结构的成员,但不能在 C 中使用。您需要将函数指针作为 C 中结构中的成员。
回答by René Nyffenegger
Ok, POD means plain old data. That usually refers to structs
withoutany methods because these types are then used to structure multiple data that belong together.
好的,POD 意味着普通的旧数据。这通常是指structs
没有任何方法,因为这些类型随后用于构造属于一起的多个数据。
As for structs not having methods: I have seen more than once that a struct had methods, and I don't feel that this would be unnatural.
至于没有方法的结构体:我不止一次看到结构体有方法,我不觉得这会不自然。
回答by knivil
1) It is the only difference in C++.
1) 这是 C++ 中的唯一区别。
2) POD: plain old dataOther classes -> not POD
2) POD:普通旧数据其他类 -> 不是 POD