struct和enum之间的区别? [英] Difference between struct and enum?

查看:1184
本文介绍了struct和enum之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,想了解说之间的区别

I am newbie to C++, and want to understand what is the difference between saying

typedef enum stateUpdateReasonCode
{
    a=1,
    b=2,
    c=3
} StateUpdateReasonCode;

struct StateUpdateReasonCode
{
   a=1,
   b=2,
   c=3
};

它们之间有什么区别?我们为什么要一个一个地使用另一个?

What is difference between them ? Wy would we use one over another ?

亲切问候

推荐答案

枚举和结构完全是不同的概念,可实现不同的目的。

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.

A struct 是一个数据结构。最简单地说,它包含零个或多个数据(变量或对象),将它们组合在一起,以便可以将它们作为一个单元进行存储,处理或传递。您通常可以拥有多个副本(或实例)。不过,一个结构可能要复杂得多。除了成员默认情况下是公开的而非私有的,它实际上与类完全相同。像类一样,结构可以具有成员函数和模板参数,等等。

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.

结构与枚举之间的重要区别之一是,在运行。仅当您读取/编写代码时才对您有利。但是,结构(和类)的实例在运行时肯定可以存在于内存中。

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 can exist in memory at runtime.

从编码的角度来看,枚举中的每个标识符都不会没有自己的类型。结构中的每个成员必须必须具有类型。

From a coding standpoint, each identifier in an enum doesn't have its own type. Every member within a struct must have a type.

这篇关于struct和enum之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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