C++ 结构体和枚举的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20680957/
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 struct and enum?
提问by AndroidDev
I am newbie to C++, and want to understand what is the difference between saying
我是 C++ 的新手,想了解这句话之间的区别
typedef enum stateUpdateReasonCode
{
a=1,
b=2,
c=3
} StateUpdateReasonCode;
and
和
struct StateUpdateReasonCode
{
a=1,
b=2,
c=3
};
What is difference between them ? Wy would we use one over another ?
它们之间有什么区别?我们为什么要使用一个?
Kind Regards
亲切的问候
回答by Peter Bloomfield
An enum and a struct are totally different concepts, fulfilling different purposes.
枚举和结构是完全不同的概念,实现不同的目的。
An enum
lets you declare a series of identifiers for use in your code. The compiler replaces them with numbers for you. It's often useful for making your code more readable and maintainable, because you can use descriptive names without the performance penalty of string comparisons. It can also make the code less bug-prone because you don't have to keep writing in specific numbers everywhere, which could go wrong if a number changes.
Anenum
允许您声明一系列标识符以在您的代码中使用。编译器为您用数字替换它们。它通常有助于使您的代码更具可读性和可维护性,因为您可以使用描述性名称而不会受到字符串比较的性能损失。它还可以使代码不那么容易出错,因为您不必到处都写特定的数字,如果数字发生变化,这可能会出错。
A struct
is a data structure. At its simplest, it contains zero or more pieces of data (variables or objects), grouped together so they can be stored, processed, or passed as a single unit. You can usually have multiple copies (or instances) of it. A struct can be a lot more complex though. It's actually exactly the same as a class, except that members are public by default instead of private. Like a class, a struct can have member functions and template parameters and so on.
Astruct
是数据结构。最简单的说,它包含零个或多个数据(变量或对象),将它们组合在一起,以便它们可以作为一个单元进行存储、处理或传递。您通常可以拥有它的多个副本(或实例)。但是,结构可能要复杂得多。它实际上与类完全相同,除了默认情况下成员是公共的而不是私有的。像类一样,结构体可以有成员函数和模板参数等。
One of the vital difference between structs and enums is that an enum doesn't exist at run-time. It's only for your benefit when you're read/writing the code. However, instances of structs (and classes) certainly canexist in memory at runtime.
结构体和枚举之间的重要区别之一是枚举在运行时不存在。当您阅读/编写代码时,这只是为了您的利益。但是,结构(和类)的实例在运行时肯定可以存在于内存中。
From a coding standpoint, each identifier in an enum doesn't have its own type. Every member within a struct musthave a type.
从编码的角度来看,枚举中的每个标识符都没有自己的类型。结构中的每个成员都必须有一个类型。
回答by pmr
The first compiles, the second does not.
第一个编译,第二个没有。
Your struct
declaration is invalid. In plain C struct
are so called record types, they contain a set of values (each with it's own type). In C++ this capability is expanded and a struct
is basically equivalent to a class
. The struct can now have base classes, member functions, access specifiers, conversion operators, operator overloads and so on.
您的struct
声明无效。在普通 Cstruct
中,所谓的记录类型,它们包含一组值(每个值都有自己的类型)。在 C++ 中,此功能得到了扩展, astruct
基本上等同于 a class
。该结构现在可以具有基类、成员函数、访问说明符、转换运算符、运算符重载等。
An enum is an enumeration type: it describes a finite set of states. In C and C++ the fact that enum values are convertible to integers is more or less a leaking abstraction.
枚举是一种枚举类型:它描述了一组有限的状态。在 C 和 C++ 中,枚举值可以转换为整数这一事实或多或少是一个泄漏的抽象。
They are fundamentally different.
它们在本质上是不同的。
回答by noelicus
Struct
结构
For a struct these values are defaults (only for C++ 11 onwards) for the data structure. A "struct" is a structure of data, for example:
对于结构,这些值是数据结构的默认值(仅适用于 C++ 11 以后)。“结构”是一种数据结构,例如:
struct Car
{
float enginesize = 0.0f;
char modelname[100];
};
You would assign these values after you've declared a variable of the type Car
etc:
在声明类型Car
等变量后,您将分配这些值:
{
Car brum = {1.0f}; // Specify a a size: don't use default
Car zoom; // Will default to 0.0 as specified in the struct
}
Without a default value it will undefined (something random).
如果没有默认值,它将是未定义的(随机的)。
Enum
枚举
An enumeration, however, is renamed numeric values: it's a very handy way of naming numeric constants. e.g.
然而,枚举被重命名为数值:这是命名数值常量的一种非常方便的方式。例如
enum EngineType
{
Petrol,
Diesel,
Electric,
LPG = 34 // NB assigning values is optional
};
回答by Atul
enum work like the constants where you want to specify the the value with a word. Like for the days of week one want that sun = 0, mon = 1 and so on. In this case enum can be used.
枚举的工作方式类似于您想要用单词指定值的常量。就像一周中的几天希望太阳 = 0,星期一 = 1 等等。在这种情况下,可以使用枚举。
struct is totally different from the enum. It can be seen analogues to the class in c++ or any other programming language. structure is a user defined data type which can be used to store the info. Like in address different fields can be there street , zip code etc.
struct 与枚举完全不同。可以看到它类似于 C++ 或任何其他编程语言中的类。结构是用户定义的数据类型,可用于存储信息。就像在地址不同的领域可以有街道,邮政编码等。
the first one compiles as it stores the value of an enum and second one does not as struct variable data members values can be assigned by creating a struct variable.
第一个编译,因为它存储枚举的值,第二个不作为结构变量数据成员可以通过创建结构变量来分配值。