为什么在C ++中存在struct和class? [英] Why do both struct and class exist in C++?

查看:175
本文介绍了为什么在C ++中存在struct和class?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道, struct class 在语言的许多地方是可以互换的。令人困惑的是,关键字本身不一定对应于标准中使用的语言。例如,在草案标准N4567 [class] / 10中,


A POD struct / sup>是一个非联合类,它既是一个
平凡类和一个标准布局类,并且没有非静态数据
类型非POD结构的成员,非POD联合或这种
类型的数组)。类似地,< em> POD联合是一个联合,它是一个简单的
类和一个标准布局类,并且没有非POD结构类型的非静态数据成员
,非POD联合(或这种类型的数组)。




>在以下情况下, struct class 可以互换:




  • 声明类

  • 范围声明枚举类型

  • 类型说明符,除非用 union

  • $ b
code> struct 不能在模板声明中使用以引入类型模板参数:

  template< struct T> //错误

我不能看到 struct class ,即使在上面的POD示例中,因为标准中定义的 POD struct code> struct 或 class


[class] / 8一个标准布局结构是一个标准布局类
使用 class-key struct class-key类。 A
标准布局联合是用 class-key union 定义的标准布局类。


$



这里有两个问题:


  1. 我有没有任何技术差异,显着区分 struct


  2. b

我忽略了默认访问说明符之间的区别,因为每个人都知道。

解决方案


为什么结构和类都存在于C ++中?



$ b b

存在 struct 的原因是为了与C兼容。



with Classes引入新关键字 class ,当你可以使用 struct 请参阅此答案以进行可信的投机。总之,这可能是因为希望强调 OOP ,其中 class 是一个广泛使用的术语。只有Stroustrup可以知道一定。


令人困惑的是,关键字本身不一定对应于标准中使用的语言


需要理解的是,的概念不是一个,并且与关键字 class



声明类有三个关键字。这些被称为类键的关键字是 class struct union 。以 class struct 声明的非联合类是完全相同的东西,除了。联合类不同于非联合类。


但是,struct不能在模板声明中使用来引入类型模板参数


C ++在不同上下文中为不同目的重用关键字。 class 关键字在类声明上下文中,与模板参数定义中的 class 关键字不完全相同。在一个上下文中等同于另一个关键字的一个关键字不会使其在所有上下文中都等效。在不同但相似的上下文中重用关键字的原因( static 是另一个例子),是为了避免引入新的关键字,这增加了与C(或更早的C ++标准)



为什么 class 关键字的原因是:重用在模板类型参数的上下文中可能是因为类是类型,因此通常用作类型参数。还有一个 typename 关键字,稍后添加,并且在模板类型参数声明中与(<几乎)可交换 class ,但也在其他地方使用(依赖类型名称),其中 class 未使用。有关链接的 answer 以及关于为此上下文中添加单独关键字的原因的摘要,请参阅此。



为什么 struct 在上下文中不作为等价物使用,你可以问。那么,这是另一个问题Stroustrup或委员会。这是一个与委员会在枚举类 / 引入enum struct 时所做的选择相反的选择。


我无法看到struct和类之间有什么显着差异


好。除了

没有任何除了


这似乎是多余的和混乱的,同时引入了明显的不一致。 / p>

我看到标准的报价没有不一致。我看到冗余,我怀疑冗余存在,以使它更加清楚,用关键字 struct 声明的类仍然是一个类。



  1. 是否有任何技术差异,我错过了显着区分结构和类?


我已经回答了,但是为了清楚起见,用 struct声明的类之间有没有



†与默认访问说明符的区别(您已经知道,并且还介绍了此处),这是他们的唯一差异。


As we know, struct and class are interchangeable in many places in the language. Confusingly, the keywords themselves do not necessarily correspond to the language used in the standard. For example, in draft standard N4567 [class]/10,

A POD struct109 is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types). Similarly, a POD union is a union that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types). A POD class is a class that is either a POD struct or a POD union.

In over-simplified terms, struct and class are interchangeable in the following cases:

  • declaration of a "class"
  • declaration of a scoped enumeration type
  • elaborated type specifier unless the "class" was declared with union

However, struct explicitly cannot be used in a template declaration to introduce type template parameters:

template <struct T> // error

I'm unable to see any significant difference between struct and class, even in the POD example above because a POD struct as defined in the standard can be declared with either struct or class.

[class]/8 A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class. A standard-layout union is a standard-layout class defined with the class-key union.

This seems rather redundant and confusing while introducing a glaring inconsistency.

I have two questions:

  1. Are there any technical differences that I have missed that significantly distinguish struct and class?

  2. What is the rationale, if any, behind this clumsiness?

I'm ignoring the difference between default access specifiers because everyone knows that already.

解决方案

Why do both struct and class exist in C++?

A reason for existence of struct is for compatibility with C.

Why then, did "C with Classes" introduce the new keyword class when you could use struct for the same thing, you may ask. See this SO answer for plausible speculation. In short, it's probably because there was desire for emphasis on OOP in which class is a widely used term. Only Stroustrup may know for certain.

Confusingly, the keywords themselves do not necessarily correspond to the language used in the standard

What needs to be understood, is that the concept of a class is not one and the same with the keyword class.

There are three keywords for declaring classes. These keywords known as class-keys are class, struct and union. The non-union classes that are declared with either class or struct are exactly the same thing, except for . Union classes are different from non-union classes.

However, struct explicitly cannot be used in a template declaration to introduce type template parameters

C++ re-uses keywords for different purposes in different contexts. class keyword in a class declaration context, is not entirely the same as class keyword in a template argument definition. One keyword being equivalent to another in one context does not make it equivalent in all contexts. The reason for reusing keywords in different but similar contexts (static is another example), is to avoid introducing new keywords, which introduces more holes with compatibility with C (or earlier C++ standard) that does not have the new keywords.

The reason why class keyword was reused in the context of template type arguments was probably because classes are types, and therefore typically used as type parameters. There is also a typename keyword, which was added later and is (almost) interchangeable with class in template type argument declaration, but also used elsewhere (dependent type names) where class is not used. See this answer for a link and a summary about why a separate keyword was added to that context.

Why struct is not used in the context as an equivalent, you may ask. Well, that's another question to Stroustrup or the committee. It's an opposite choice than what committee did when enum class/enum struct was introduced.

I'm unable to see any significant difference between struct and class

Good. There isn't any except for

This seems rather redundant and confusing while introducing a glaring inconsistency.

I see no inconsistency in the quote from the standard. I see redundancy and I suspect that the redundancy exists to make it extra clear that a class declared with the keyword struct is still a class.

  1. Are there any technical differences that I have missed that significantly distinguish struct and class?

I've already answered, but to be clear, there is no difference between classes declared with struct and class keywords, beyond .

the difference with the default access specifier (as you already know, and also described here), which is their only difference.

这篇关于为什么在C ++中存在struct和class?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆