Difference between struct and enum?

后端 未结 4 1274
名媛妹妹
名媛妹妹 2020-12-14 19:06

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

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


        
相关标签:
4条回答
  • 2020-12-14 19:30

    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 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.

    0 讨论(0)
  • 2020-12-14 19:43

    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:

    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 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
    };
    
    0 讨论(0)
  • 2020-12-14 19:46

    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.

    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.

    They are fundamentally different.

    0 讨论(0)
  • 2020-12-14 19:52

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题