C ++中的struct和class有什么区别?
这个问题已经在C#/。Net的上下文中提出过。
现在,我想学习C ++中的结构与类之间的区别。请讨论技术差异以及在OO设计中选择一个或者另一个的原因。
我将从一个明显的区别开始:
- 如果我们未指定
public:
或者private:
,则默认情况下结构的成员是public;默认情况下,类的成员是私有的。
我确信在C ++规范的晦涩之处可以找到其他差异。
解决方案
默认情况下,班级成员是私人的。默认情况下,Struct的成员是公开的。除此之外,没有其他差异。另请参阅此问题。
这是一个很好的解释:http://carcino.gen.nz/tech/cpp/struct_vs_class.php
So, one more time: in C++, a struct is identical to a class except that the members of a struct have public visibility by default, but the members of a class have private visibility by default.
不在规格中,否。主要区别在于程序员在两年内阅读代码时的期望。结构通常被认为是POD。当我们出于定义对象的目的而定义类型时,结构元数据也用于模板元编程中。
根据Stroustrup的C ++编程语言:
Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures."
在功能上,除了公共/私人之外没有其他区别
唯一的不同是类和结构的默认继承,毫不奇怪,它们分别是私有和公共的。
这只是一个约定。可以创建结构来保存简单数据,但是以后通过添加成员函数和构造函数来发展时间。另一方面,除了公开之外,看不到其他任何东西:结构中的访问。
引用C ++常见问题解答,
[7.8] What's the difference between the keywords struct and class? 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.
STRUCT是一种抽象数据类型,它根据结构规范对给定的内存块进行划分。结构在文件序列化/反序列化中特别有用,因为结构通常可以逐字写入文件中。 (即获取指向该结构的指针,使用SIZE宏计算要复制的字节数,然后将数据移入或者移出该结构。)
类是尝试确保信息隐藏的另一种抽象数据类型。在内部,可以有各种各样的机器,方法,临时变量,状态变量。等等,都用于向希望使用该类的任何代码提供一致的API。
实际上,结构与数据有关,类与代码有关。
但是,我们确实需要了解这些仅仅是抽象。完全有可能创建看起来很像类的结构和看起来很像结构的类。实际上,最早的C ++编译器只是将C ++代码转换为C的预编译器。因此,这些抽象对逻辑思维是有益的,而对计算机本身不一定是一种资产。
除了每个都是不同类型的抽象这一事实之外,类还为C代码命名难题提供了解决方案。由于我们不能公开多个具有相同名称的函数,因此开发人员通常会遵循_()模式。例如mathlibextreme_max()。通过将API分组为类,可以将相似的函数(这里称为"方法")组合在一起,并防止其他类中方法的命名。这使程序员可以更好地组织代码并增加代码重用。从理论上讲,至少。
需要注意的另一件事是,如果更新了具有结构以使用类的结构的旧版应用程序,则可能会遇到以下问题:
旧代码具有结构,代码已清理,并将这些更改为类。
然后将一个或者两个虚拟函数添加到新的更新的类中。
当虚拟函数位于类中时,则在内部编译器将添加额外的指向类数据的指针以指向这些函数。
这将如何破坏旧的旧代码,如果在旧代码中某个地方使用memfill将结构清除为零,则这也会占用额外的指针数据。
- 结构的成员默认情况下是公共的,类的成员默认情况下是私有的。
- 从另一个结构或者类的结构的默认继承是公共的。从另一个结构或者类的结构的默认继承是私有的。
class A{ public: int i; }; class A2:A{ }; struct A3:A{ }; struct abc{ int i; }; struct abc2:abc{ }; class abc3:abc{ }; int _tmain(int argc, _TCHAR* argv[]) { abc2 objabc; objabc.i = 10; A3 ob; ob.i = 10; //A2 obja; //privately inherited //obja.i = 10; //abc3 obss; //obss.i = 10; }
这是在VS2005上。
我们会忘记类和结构之间的棘手的第二个区别。
符合标准(C ++ 98到C ++ 11中的11.2.2):
In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.
出于完整性的考虑,在(11.2)中定义了class和struct之间更广为人知的区别:
Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct or union are public by default.
另外的区别:关键字" class"可用于声明模板参数,而" struct"关键字则不能如此使用。
ISO IEC 14882-2003 9 Classes §3 A structure is a class defined with the class-key struct; its members and base classes (clause 10) are public by default (clause 11).
值得记住C ++的起源和与C的兼容性。
C具有结构,没有封装的概念,因此所有内容都是公共的。
默认情况下,默认情况下公开公开是在采用面向对象的方法时的一个坏主意,因此在制作一种本来有利于OOP的C形式(我们可以在C中进行OO,但无济于事)时,在C ++中(最初是" C With Classes")的想法,默认情况下将成员设为私有是有意义的。
另一方面,如果Stroustrup更改了struct的语义,以便其成员默认情况下是私有的,则兼容性将受到破坏(不再像标准出现那样多了,但是所有有效的C程序也都有效C ++程序,这对C ++的立足点有很大的影响)。
因此引入了一个新的关键字" class",它与结构完全一样,但默认情况下为private。
如果C ++是从头开始的,没有历史,那么它可能只有一个这样的关键字。它也可能不会产生影响。
通常,人们在进行诸如在C语言中使用结构的操作时会倾向于使用struct。公共成员,没有构造函数(只要不是联合体,就可以在结构中构造构造函数,就像使用类一样,但是人们通常不会这样做),没有虚拟方法等。人们阅读代码以指示机器(否则我们会坚持使用汇编代码和原始VM操作码),这是一个好主意。