Thread: Assigning enum types to enum types?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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