Difference between struct and enum?

I am new to C ++ and want to understand what is the difference between words

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

and

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

What is the difference between the two? Would we use one by one?

Yours faithfully

+7
c ++
source share
4 answers

Enumeration and structure are completely different concepts that fulfill different goals.

An enum allows you to declare a series of identifiers for use in your code. The compiler replaces them with numbers for you. This is often useful in order to make your code more readable and maintainable, because you can use descriptive names without performance limitation to compare strings. It can also make the code less error prone, because you donโ€™t need to constantly write on certain numbers, which may go wrong if the number changes.

A struct is a data structure. In its simplest case, it contains zero or more pieces of data (variables or objects) grouped together so that they can be stored, processed or transmitted as a whole. You can usually have multiple copies (or instances). However, the structure can be much more complicated. This is actually exactly the same as a class, except that members are public by default and not private. Like a class, a structure can have member functions and template parameters, etc.

One of the significant differences between structures and enumerations is that an enumeration does not exist at run time. This is only for your benefit when you read / write code. However, instances of structures (and classes) can certainly exist in memory at run time.

From an encoding point of view, each identifier in an enumeration does not have its own type. Each member within the structure must have a type.

+10
source share

The first is compiled, the second is not.

Your struct declaration is invalid. In simple C struct , the so-called record types are called; they contain a set of values โ€‹โ€‹(each with its own type). In C ++, this feature expands, and struct is basically equivalent to class . Now the structure can have base classes, member functions, access specifiers, conversion operators, operator overloads, etc.

An enumeration is a type of enumeration: it describes a finite set of states. In C and C ++, the fact that enum values โ€‹โ€‹are converted to integers is more or less an abstraction leak.

They are fundamentally different.

+9
source share

Your "structure" is not a structure and will not compile - you cannot assign in-situ values โ€‹โ€‹to it. This is supposed to be a data structure, for example:

 struct Car { float enginesize; char modelname[100]; }; 

You assigned these values โ€‹โ€‹after you declared a variable of type Car , etc.

An enumeration, however, is renamed to numerical values: this is a very convenient way of naming numerical constants.

+2
source share

enum work like constants where you want to specify a value with a word. As on the days of the week, it is necessary that the sun = 0, mon = 1 and so on. In this case, you can use an enumeration.

Structure

completely different from the listing. It can be seen analogues with a class in C ++ or any other programming language. A structure is a user-defined data type that can be used to store information. As in the address, in different fields there may be a street, zip code, etc.

the first is compiled because it stores the value of the enumeration, and the second does not matter, since the values โ€‹โ€‹of the data elements of the structural variable can be assigned by creating the structural variable.

+2
source share

All Articles