关于c ++:结构和类之间的区别

Difference between a struct and a class

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
What are the differences between struct and class in C++

我做过作业,在谷歌上有各种各样的答案。有些人说结构没有继承,有些人说结构没有访问说明符,而另一些人说它们都有。有人可以澄清,C和C++中结构和类之间的区别,以及C++C++中结构的区别。


在C++中,结构和类之间唯一的区别是结构成员默认是公共的,默认情况下类成员是私有的。

但是,作为风格的问题,最好使用EDCOX1的2个关键字,以便在C(或多或少的POD类型)中合理地构造一个结构,并且如果使用C++特定的特征(如继承和成员函数),则EDCOX1×3关键字。

C没有课程。

C结构不能使用C++特定的特性。

编辑:

C++ FAQ Lite,问题7.9,有这样的说法:

The members and base classes of a struct are public by default,
while in class, they default to private. Note: you should make
your base classes explicitly public, private, or protected,
rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

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的《C++程序设计语言》,第四版,第16.2.4节:

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.


在C中,类不存在。在C++中,Struts具有EDCOX1×0的默认访问说明符,而类默认为EDCOX1(1)。

C和C++中的结构有几个不同点;在C++中,它们可以从其他类或结构继承,它们可以包含成员函数,而它们的名称不需要用详细的类型说明符来引用。