Thread: Assigning enum types to enum types?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    53

    Assigning enum types to enum types?

    Hello,

    Simple question. I would like to confirm something about enum types. I always thought that when assigning a value from one variable type to another, a cast is required unless its an implicit cast:

    http://www.cplusplus.com/doc/tutorial/typecasting/


    I was just wondering if assigning one enum parameter of type A to another enum variable of type B is considered an implicit cast aswell?

    If I do this, I don't seem to get any compile errors and yet an assignment is made between two different types:

    Code:
    #include <stdio.h>
    
    enum A{one=1, two, three};
    enum B{Dog =100, cat, bird};
     
    int main()
    {
    enum B x;
    
    x = one;
    
    }

    The reason I ask is that I am doing this sort of stuff lately where enums are assigned to each other and they are of different types.

    In conclusion, I would like to know as to why the above works... is it because enum types are implicit and a cast is done automatically for us... or is it that because by nature enums are of int type and there really is no need for casting them. Also, it happens that I assign an enums value to a short... and that works as well? In reference to above code.. like this:

    unsigned short s;

    s = one;

    In my code project, everything works, but I am being ignorant as to what is really happening. I am sort of just brushing it under the carpet so of speak. And thats no good. I apologize for the simple question. Any feedback is much appreciate.

    Rob
    Last edited by see the big C; 12-19-2010 at 11:49 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by see the big C View Post
    Hello,

    Simple question. I would like to confirm something about enum types. I always thought that when assigning a value from one variable type to another, a cast is required unless its an implicit cast:

    Type Casting
    Hi Rob... I'm no expert on enums so we'll let the others tackle that.

    However; I would caution you against trying to learn C from a C++ tutorial. There are some strong similarities, but these actually are 2 different languages.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    enums are integral types, hence the integer promotions will come into play. Generally, an enum has the same "size" as either a char or an int, depending on which one has enough room to store all the listed values. When you do an assignment like above, the value on the right is widened (if necessary) to be the same size as the thing on the left, and then the assignment happens as integers.

    That is to say, it's the same sort of thing that happens when you do
    Code:
    char foo = 3;
    since both char and int are integral types, the assignment goes through (in this case, truncating the extra bits on the left). The compiler doesn't check that the result is a legitimate value of the enum.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Hi tabstop,

    Thanks for your feedback. But I heard that this code sample in C++ would not work.
    Is this true?

    r

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. In C++ convert to int is fine; from int requires an explicit cast.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    yeah, like this right?

    Code:
    #include <stdio.h>
    
    enum A{one=1, two, three};
    enum B{Dog =100, cat, bird};
     
    int main()
    {
    enum B x;
    x = (enum B) one;   //<< explicit cast!
    }
    thanx
    Last edited by see the big C; 12-19-2010 at 08:11 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    enum's in C are simply named integer constants, so they are (implicitly) convertible to each other (assuming the variable getting the result can represent the value being assigned). However, an explicit conversion is generally considered good style - in the interest of clarity. It can otherwise be a bit disconcerting when an assignment of seen of the form "month = Monday".

    In C++ they are not convertible to each other without an explicit conversion.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by grumpy View Post
    In C++ they are not convertible to each other without an explicit conversion.
    Or an overloaded operator!
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    53
    Thankyou all for your help!
    rob

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by see the big C View Post
    yeah, like this right?

    Code:
    #include <stdio.h>
    
    enum A{one=1, two, three};
    enum B{Dog =100, cat, bird};
     
    int main()
    {
    enum B x;
    x = (enum B) one;   //<< explicit cast!
    }
    thanx
    Well, not quite (at least not ideally).
    In C++ you don't need to put enum or struct in front of where the type is used, so just (B) would suffice. However the proper C++ way is to use static_cast.
    Code:
    #include <stdio.h>
    
    enum Number {one=1, two, three};
    enum Animal {Dog=100, cat, bird};
     
    int main()
    {
        Animal x = static_cast<Animal>(one);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sipher View Post
    Or an overloaded operator!
    To implement that overloaded operator, it is necessary to either use an explicit conversion or some form of explicit mapping between values of the two enumerated types (which, logically, is just an explicit conversion by another method).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using scanf with enum types
    By bhdavis1978 in forum C Programming
    Replies: 8
    Last Post: 11-11-2010, 12:39 PM
  2. Replies: 47
    Last Post: 07-13-2010, 07:22 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. enum types
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-31-2002, 08:14 AM