Casting to an Enum

This is a discussion on Casting to an Enum within the C++ Programming forums, part of the General Programming Boards category; This section of code was taken out of a program that assigns an array of 52 card objects a number ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Casting to an Enum

    This section of code was taken out of a program that assigns an array of 52 card objects a number and a suit (a deck of cards). Then it displays the ordered deck, shuffles it and then displays the shuffled deck.

    Heres the enum:
    Code:
     enum Suit { clubs, diamonds, hearts, spades };
    Here's the excerpt of code that my question lies. My problem is highlighted in red:
    Code:
    ...
    void main()
    {
    	card deck[52];
    
    	cout << endl;
    	for(int j=0; j<52; j++)
    	{
    		int num = (j % 13) + 2;   // cycles through 2 to 14, 4 times
    		Suit su = Suit(j / 13);    // cycles through 0 to 3, 13 times
    		deck[j].init(num, su);  // sets card member data
    	}
    }...
    I don't understand why (j / 13) is casted. Wouldn't
    j / 13 be the same without the Suit cast?
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Wouldn't j / 13 be the same without the Suit cast?
    Try removing it and see.
    My best code is written with the delete key.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > void main()
    Not your only mistake, main returns int

    > Wouldn't j / 13 be the same without the Suit cast?
    Whilst enums are represented using ints, an int isn't an enumeration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    dang i forgot to change my void main before posting, ok..don't comment on it again, it's over.

    Salem, but you can assign and enum an integer value Ex.
    Suit su = 0 is the same as
    Suit su = clubs;
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    ....or anyone else could anser, even though i posted salem..
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    enum Suit { clubs = 1, diamonds = 2, hearts = 4, spades = 8 };

    Which one still works?
    Suit su = 0; // this one
    Suit su = clubs; // or this one

    Casting won't help you with this one either.

    The point is, you might "know" that the enumerations have the values 0 to 3 and your calculation returns values 0 to 3, but that doesn't make it an automatic thing as far as the compiler is concerned.


    If you don't want to be hampered by type safety, then do
    #define clubs 0
    #define diamonds 1
    But I wouldn't recommend that for C++ code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. Casting int to enum problem
    By jw232 in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2009, 09:40 AM
  3. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 09:29 PM
  4. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 12:16 PM
  5. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM

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