结构和类之间的区别 [英] Difference between a struct and a class
问题描述
我已经完成作业,并在Google上得到了各种各样的答案.有些人说结构没有继承,有些人说结构没有访问说明符,而另一些人说它们都具有访问说明符.
有人可以澄清一下,C和C ++中的结构与类之间的差异,以及C&中的结构与结构之间的差异.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 ++中,结构和类之间的唯一区别是结构成员默认情况下是公共的,而类成员默认情况下是私有的.
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.
然而,就样式而言,最好将 struct
关键字用于可能合理地为C结构(或多或少的POD类型)的东西,以及 class
关键字(如果它使用特定于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.
C没有类.
C结构不能使用C ++特有的功能.
C structs cannot use C++-specific features.
编辑:
C ++ FAQ Lite (问题7.9)说:
默认情况下,
struct
的成员和基类是public
,而在class
中,它们默认为private
.注意:您应该您的基类显式地public
,private
或protected
,而不是依赖默认值.
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
和 class
在功能上是等效的.
struct
and class
are otherwise functionally equivalent.
好的,足够的干净利落的技术讨论.在情感上,大多数开发人员在 class
和 struct
之间进行了强烈区分. struct
简单地感觉就像一堆开放的比特,几乎没有以封装或功能的方式.一个 class
feel 像富有智慧的服务,是一个活泼且负责任的社会成员,强大的封装屏障,以及定义明确的界面.自从这就是大多数人已经拥有的含义,您可能应该如果您有一个方法很少的类,请使用 struct
关键字并具有 public
数据(在精心设计的环境中存在要做系统!),否则您可能应该使用 class
关键字.
OK, enough of that squeaky clean techno talk. Emotionally, most
developers make a strong distinction between a class
and a struct
.
A struct
simply feels like an open pile of bits with very little
in the way of encapsulation or functionality. A class
feels like 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 the struct
keyword if you have a class that has very few methods
and has public
data (such things do exist in well designed
systems!), but otherwise you should probably use the class
keyword.
并引用Stroustrup的"The C ++ Programming Language",第4版,第16.2.4节:
And quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4:
S 的这两个定义可以互换,尽管通常明智的做法是坚持一种风格.您使用哪种样式取决于情况和品味.我倾向于对我使用的类使用 struct 认为是只是简单的数据结构".如果我认为一个班级是一个具有不变式的适当类型,"我使用 class .构造函数和即使对于* struct * s,访问函数也可能非常有用,但是作为一个速记而不是保证不变式.
These two definitions of S are interchangeable, though it is usually wise to stick to one style. Which style you use depends on circumstances and taste. I tend to use struct for 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.
这篇关于结构和类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!