How do they help with coding? They don't seem like it makes the code shorter so I'm confused... maybe I'm doing it wrong but...
could anyone explain to me about enums and structures please?
It'd be greatly appreciated, Merry Christmas!

Recommended Answers

All 9 Replies

Who ever said that enums and structures are supposed to make the code shorter??

Who ever said that enums and structures are supposed to make the code shorter??

No one, I just assumed it lol. oops...
Could you explain what they are supposed to do?

structs are there in C++ for backwards compatibility.
Enums are there to help you with constants and a new( but rather old) technique to help in metaprogramming.

But lets go back to the constants thing. Which would you prefer?

struct{
 enum Direction{ EAST, SOUTH, WEST, NORTH }
//...
};

//or
struct{
 const int EAST = 0;
 const int SOUTH = 1;
 const int WEST = 2;
 const int NORTH = 3;
};

IMO, the enum version is better because not only is the enum more descriptive because of the name you can give it( Direction in the above case ), but it keeps count for you.

Structures are intended for grouping data together, which can then be worked on as a unit - you can have pointers to structures, pass them as function arguments and returns, etc. Structures make it feasible to work on more complex data structures such as linked lists, trees, heaps, and so forth.

As FirstPerson says, they are primarily in C++ because they had been in C originally, and C++ started out as a super-set of C. The role of structures has largely been superseded by classes, which have the same basic purpose but have additional features for supporting object-oriented programming (e.g., inheritance).

The role of structures has largely been superseded by classes, which have the same basic purpose but have additional features for supporting object-oriented programming (e.g., inheritance).

Sigh. That is incorrect. Classes and structs are no different in C++ except for the fact that by default, structs uses 'public' and classes uses 'private' as an access specifier.

So, there is only a very trivial difference between a struct and a class . And struct was kept for backward compatibility with C, while class was introduced because it is generally more appropriate for Object-Oriented Programming purely on the basis of vocabulary (i.e. in OOP with about "classes" of objects) and on the grounds of default access rights and inheritance (private) which is more in-line with OOP design philosophy. But, in practical terms, the difference between struct and class remains trivial.

What you call "structures" is what experienced programmers colloquially refer to as "POD-types" (Plain-Old-Data types). These are basically simple little bundle of data like:

struct Vector2d {
  double x;
  double y;
};

By convention, in C++, people use the keyword struct when creating such simple classes, and thus, people sometimes call them "structures", but I tend to dislike this awkward, overly general wording.

So, the purpose of POD-types is similar to that of any other class, except that, in this case, you don't have any functionality to associate directly to the class (or very little functionality). In other words, when you just want to bundle a bunch of data elements together, not much more. POD-types are a special kind of "value-objects" (or value-classes).


>>How do they help with coding?

Well POD-types help with coding just because data structures are omni-present in programming tasks and very often all you need is to group a number of variables together as one logical entity, and that's it.

Enums simply allow you to associate identifiers to the elements of an enumeration of choices for a particular purpose. Like in firstPerson's example, you have 4 choices for the purpose of telling a direction: north, south, east, and west. Enums are a nice way to make human-readable code that is neat and safe.

Of course, both enums and structs don't serve such a special purpose that no other thing could do the same job, they are just two more tools in the tool-box.


>>They don't seem like it makes the code shorter so I'm confused

Making code shorter is rarely a concern for any programmer (I mean, not uselessly repeating large sections of code is certainly a concern, but compressing code to the absolute minimal amount of lines is just plain stupid). Programmers are by far more interested in making neat, human-readable code. You often will hear things like "code should read like prose", "correctness, simplicity, and clarity come first", or "prefer long descriptive names for variables to short accronyms", etc. This is all because generally good code is easy to read and understand without even having any comments explaining it. Many programming languages were designed with these prime concerns in mind. Tools like struct and enum are simply nice and simple, and intuitive constructions that are tailored to a very common purpose in programming.

N.B. One very important advantage of enums over constant values is that they are type-safe, as in firstPerson's example, when using a Direction enum, as a parameter to a function or something, the type-safety of the enum (almost) guarantees that the caller will not provide a value other than the three values that are part of the enum's definition.

Sigh. That is incorrect. Classes and structs are no different in C++ except for the fact that by default, structs uses 'public' and classes uses 'private' as an access specifier.

cringes at own mistake You are correct, of course, and I should have been more careful in what I wrote.

sigh I make so many mistakes of this sort, I wonder why I even post here if I can't be more accurate...

commented: All good, live and learn bro +13
struct{
const int EAST = 0;
const int SOUTH = 1;
const int WEST = 2;
const int NORTH = 3;
};

Who in God's name would use a struct like that? That is not what structs are there for.

The enum keyword is for where you want to have different, related constants enumerated without having to use #define on everything. Logically, it should only be used when the number behind that constant has no real meaning. (For example, nobody should care that direction_north is 2, or 3, or -45, because the code should only really refer to it as direction_north ). If you have to refer directly to a value like that, you're probably using enum incorrectly.

Here is an example of how I used an enum in a game I am currently programming.

//enum for statuses returned by loadgame()
enum loadstatus_t
{
    load_success,     //Indicates that the savegame was successfully loaded.
    load_nofile,      //Indicates the save doesn't exist.
    load_badversion,  //Indicates the save is from too new or old of a version.
    load_smallscreen  //Indicates the user's screen dimensions can't fit the save file on screen
};

A struct should be created when you have several variables that 'go together'. For example, you can have this mess:

float redapple_weight;
float redapple_diameter;
int redapple_price;
bool redapple_isrotting;

float greenapple_weight;
float greenapple_diameter;
int greenapple_price;
bool greenapple_isrotting;

float crabapple_weight;
float crabapple_diameter;
int crabapple_price;
bool crabapple_isrotting;

See how much of a mess that is? You basically have to copy-paste a lot of code, and if you wanted pass all of these related variables to a function, you'd have an argument list a mile long! It makes much more sense to do this:

struct apple
{
    float weight;
    float diameter;
    int price;
    bool isrotting;
};

apple redapple;
apple greenapple;
apple crabapple;

If you compare the number of lines here, the struct keyword actually does help keep your code shorter.

>>Who in God's name would use a struct like that? That is not what structs are there for.

It was an example, albiet not a complete one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.