C++ 结构体和类的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7762085/
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
Difference between a struct and a class
提问by Siddhartha
Possible Duplicate:
What are the differences between struct and class in C++
I've done my homework and had diverse answers on Google.
Some say structs do not have inheritance, some say structs do not have access specifiers, while others say they have both.
Could someone clarify then, the differences between a struct and a class in C and C++, and also the difference between a struct in C & C++.
我做完了功课,在谷歌上得到了各种各样的答案。有人说结构没有继承,有人说结构没有访问说明符,而其他人说它们两者都有。
有人可以澄清一下,C 和 C++ 中结构体和类之间的区别,以及 C 和 C++ 中结构体之间的区别。
回答by Keith Thompson
In C++, the only difference between a struct and a class is that struct members are public by default, and class members are private by default.
在 C++ 中,结构体和类之间的唯一区别是结构体成员默认是公共的,而类成员默认是私有的。
However, as a matter of style, it's best to use the struct
keyword for something that could reasonably be a struct in C (more or less POD types), and the class
keyword if it uses C++-specific features such as inheritance and member functions.
但是,就风格而言,最好将struct
关键字用于可能合理地是 C 中的结构(或多或少 POD 类型)的内容,class
如果它使用 C++ 特定的功能(例如继承和成员函数),则最好使用关键字。
C does not have classes.
C 没有类。
C structs cannot use C++-specific features.
C 结构不能使用 C++ 特定的功能。
EDIT:
编辑:
The C++ FAQ Lite, question 7.9, has this to say:
在C ++ FAQ精简版,7.9的问题,有这样一段话:
The members and base classes of a
struct
arepublic
by default, while inclass
, they default toprivate
. Note: you should make your base classes explicitlypublic
,private
, orprotected
, rather than relying on the defaults.
struct
andclass
are otherwise functionally equivalent.OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a
class
and astruct
. Astruct
simply feelslike an open pile of bits with very little in the way of encapsulation or functionality. Aclass
feelslike a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use thestruct
keyword if you have a class that has very few methods and haspublic
data (such things doexist in well designed systems!), but otherwise you should probably use theclass
keyword.
成员和的基类
struct
是public
通过默认,而在class
,它们默认private
。注意:您应该明确地创建基类public
,private
, 或protected
,而不是依赖于默认值。
struct
并且class
在其他方面在功能上是等效的。好的,足够干净的技术谈话。在情感上,大多数开发人员在 a
class
和 a之间做出了很大的区分struct
。一个struct
简单的感觉像一个露天堆放位很少在封装或功能的方式。Aclass
感觉就像是一个有生命和负责任的社会成员,拥有智能服务、强大的封装屏障和明确定义的接口。由于这是大多数人已经拥有的含义,struct
如果您的类只有很少的方法和public
数据(这样的东西确实存在于设计良好的系统中!),您可能应该使用class
关键字,否则您可能应该使用关键字。
And quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4:
并引用 Stroustrup 的“The C++ Programming Language”,第 4 版,第 16.2.4 节:
These two definitions of Sare interchangeable, though it is usually wise to stick to one style. Which style you use depends on circumstances and taste. I tend to use structfor classes that I think of as "just simple data structures." If I think of a class as "a proper type with an invariant," I use class. Constructors and access functions can be quite useful even for *struct*s, but as a shorthand rather than guarantors of invariants.
S 的这两个定义是可以互换的,尽管坚持一种风格通常是明智的。您使用哪种风格取决于环境和品味。我倾向于将struct用于我认为“只是简单的数据结构”的类。如果我认为一个类是“具有不变量的适当类型”,我会使用class。构造函数和访问函数甚至对于 * struct*s也非常有用,但作为速记而不是不变量的保证。
回答by Hugh
In C, classes do not exist. In C++, structs have a default access specifier of public
, while classes default to private
.
在 C 中,类不存在。在 C++ 中,结构的默认访问说明符为public
,而类默认为private
。
There are several differences between structs in C and C++; in C++ they can inherit from other classes or structs, they can contain member functions, and their names don't need to be referred to with an elaborated type specifier.
C 和 C++ 中的结构体之间存在一些差异;在 C++ 中,它们可以从其他类或结构继承,它们可以包含成员函数,它们的名称不需要用详细的类型说明符引用。