NOOB problems.. again. enums..

This is a discussion on NOOB problems.. again. enums.. within the C++ Programming forums, part of the General Programming Boards category; so I think i understand enums, but not quite seeing the use for them in code... Code: enum TreeType { ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    21

    NOOB problems.. again. enums..

    so I think i understand enums, but not quite seeing the use for them in code...

    Code:
    enum TreeType
     {
           maple, douglasFir, Alder
    };

    i know that the first enum is 0, unless otherwise defined, and the next enum is the last enum + 1.
    however I am kind of struggling understanding the USE for enums.. is there a beneficial difference between me defining each tree type individually versus using an enum? ( sorry for the super simplistic example.. maybe someone could provide a more useful example for enums?)

    Code:
    int maple = 0;
    int douglasFir = 1;
    int Alder = 2;
    seems so simple, but I'm just not seeing what i would use enums for in code.. sorry for the nooby question, hopefully you guys can help get me over this feeble bump on the road of programming..
    any help or input would be appreciated, just looking for a different perspective to make this "click" in my head.
    cheers

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I think that i am noob too,but i would say that enums are not necessary .One reason that enums may be usefull,is that in your example you have only 3 things,but in a bigger example with 100 things in it,enum would be more usefull than the second part of code you wrote.However a more experienced programmer can state something else

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    right, i figured they would be helpful for larger amounts of data, but even still, is there that big of a difference between listing 100 variables in an enum versus listing 100 individually? i feel like its more for organizations sake..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Mike Beal
    however I am kind of struggling understanding the USE for enums.. is there a beneficial difference between me defining each tree type individually versus using an enum?
    Well...
    • Since the numbers don't actually matter, letting the compiler assign them for you is convenient.
    • You have a type named TreeType rather than just plain int. Unfortunately, you can still assign ints to TreeType variables, and vice versa, without a compiler warning.
    • The enumeration names are grouped together in the enum, indicating that they are related.


    Quote Originally Posted by Mike Beal
    Code:
    int maple = 0;
    int douglasFir = 1;
    int Alder = 2;
    seems so simple
    But not the same, since you did not declare them const.

    That said, if you want class constants, using static const integer members would be good since you specify exactly what integer type to use and the grouping of names is implied in them being members of a class. If necessary, you could even write a class to model an enumeration of values, and hence also get stronger type safety since objects of that class cannot be assigned to variables of other types without a suitable conversion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by laserlight View Post
    ...You have a type named TreeType rather than just plain int. Unfortunately, you can still assign ints to TreeType variables, and vice versa, without a compiler warning...
    Well, unless you use C++11's enum class, at least.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    21
    ok so at times it is for convenience, and its also when you want variables to be 'related'... you say the numbers don't matter, how so? how else would the variables be defined or recognized if they had no numbers?
    Code:
    int x = 5 /// <---is 5 NOT a constant?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Mike Beal
    ok so at times it is for convenience, and its also when you want variables to be 'related'... you say the numbers don't matter, how so? how else would the variables be defined or recognized if they had no numbers?
    Why is maple == 0, douglasFir == 1 and Alder == 2? Why not Alder == 0, maple == 1 and douglasFir == 2? Or Alder == 123, douglasFir == 456 and maple == 789? So in this case, the numbers don't matter; what matters is that they are different. In other cases, the numbers might matter, in which case this automatic assignment of numbers is not an advantage because you'll be manually assigning them anyway.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    Hm
    awesome!
    yA NO HOW TO CUT A TREE

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by laserlight View Post
    Well...
    • Since the numbers don't actually matter, letting the compiler assign them for you is convenient.
    • The enumeration names are grouped together in the enum, indicating that they are related.

    But not the same, since you did not declare them const.

    That said, if you want class constants, using static const integer members would be good since you specify exactly what integer type to use and the grouping of names is implied in them being members of a class. If necessary, you could even write a class to model an enumeration of values, and hence also get stronger type safety since objects of that class cannot be assigned to variables of other types without a suitable conversion.
    So this is why enums are useful?They have no more advantages?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Don't over think it. Enums come in handy sometimes. I wouldn't implement a finite state machine without one, but I guess it really is up to the individual.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,284
    Quote Originally Posted by std10093 View Post
    So this is why enums are useful?They have no more advantages?
    Laserlight actually gave a complete response, but clearly you failed to draw the critical conclusion about type safety.

    With enums, you will get error messages if you do
    Code:
    enum TreeType
    {
           maple, douglasFir, Alder
    };
    
    enum DogType
    {
         labrador, poodle
    };
    
    int main()
    {
        TreeType some_tree;
        some_tree = poodle;    //   Oops!
    }
    But the compiler will not complain at all if you do
    Code:
    /*  Tree types */
    
    const int maple = 0;
    const int dougladFir = 1;
    const int Alder = 2;
    
    /*  Dog types */
    
    const int labrador = 0;
    const int poodle = 1;
    
    int main()
    {
        TreeType some_tree;
        some_tree = poodle;     //  Oops!
    }
    And, since it is not unrealistic to believe that code concerned with dogs might interact with code concerned with trees..... dogs behaving as they do and all ......
    Last edited by grumpy; 07-30-2012 at 04:57 AM.
    Right 98% of the time, and don't care about the other 3%.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by grumpy View Post
    type safety.
    Sure that is an advantage I did not got it at once bu Laserlight's answer,so thanks for the example!

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I think of it more as a kind of documentation. When I see enum in code someone else wrote, I automatically assume it's a pseudo-type with a predefined list of possible values. No comments required.

    In professional software development, where people are too lazy to write comments for the most part (unless the code is very complex), that's very important. With enums, people will understand your code a lot faster.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It's the same as structs vs classes. In C++ they are exactly the same except for default access. But people generally only use structs for data collections with no methods, and classes for objects that can have actions done to them.

    Again, (almost) no technical difference, but self-documenting code makes the reader's job easier.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Type safety catches more errors at compile time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serialization of Enums
    By khdani in forum C# Programming
    Replies: 1
    Last Post: 04-27-2009, 10:57 AM
  2. Using enums in classes
    By eaane74 in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2007, 02:00 PM
  3. Noob + Functions == Problems
    By Inao in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2007, 04:02 PM
  4. printing enums
    By xddxogm3 in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2005, 12:08 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21