How does static_cast work?

This is a discussion on How does static_cast work? within the C++ Programming forums, part of the General Programming Boards category; I am making a poker game. I have it halfway done, but I don't like the way it looks. As ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    How does static_cast work?

    I am making a poker game. I have it halfway done, but I don't like the way it looks. As of now when someone gets a Queen of Hearts the output is QH. Now I know that the static_cast values from 3-6 contain the heart, diamon, club, spade shapes. I tried setting them up in an array but it gives me errors. How can I use the static_cast to display the shape of the card type?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I am not sure you would need to use a static_cast. I am making a blackjack game and I just use a char array declared and initialized as follows.
    Code:
    char suit[4]={3,4,5,6};
    Then I just
    Code:
    cout<<suit[0]; //outputs  Heart
    To do a static_cast I would guess you could do the following
    Code:
    char suit;
    int i[4]={3,4,5,6};
    suit=static_cast<char>(i[0]);// or just static_cast<char>(3);
    cout<<suit;
    Last edited by manofsteel972; 08-13-2004 at 03:55 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Post your code RealityFusion. I am rather confused as to exactly what you are doing.

    manofsteel972, your static_cast will work, but not really as advertised. What that will do is print out the character with an (in this case, ASCII) value of 3. I've not seen the ASCII table in a while, but I don't believe that that particular one is printable.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    I don't have the code on this computer. Here is what the array looks like though.
    Code:
    char suit[4] = {static_cast (3), static_cast(4), static_cast(5), static_cast(6}};
    It gives me all types of invalid errors.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Well, manofsteel gave you the correct syntax for using static_cast.

    If you need more examples of what it does, try this
    Code:
    #include <iostream>
    int main()
    {
      char suit[4] = {3,4,5,6};
      int  i[4]    = {3, 4, 5, 6};
      char isuit;
      char jsuit;
    
      isuit = static_cast<char>(i[0]);
      jsuit = (char)i[0];
    
    
      std::cout << "Using char array: ";
      std::cout << "suit[0] = " << suit[0] << std::endl << std::endl;
    
      std::cout << "Using char that was static_cast from int: ";
      std::cout << "isuit = " << isuit << std::endl << std::endl;
    
      std::cout << "Using c-style cast to char: ";
      std::cout << "(char)i[0] = " << (char)i[0] << std::endl << std::endl;
    
      std::cout << "Using char variable assigned with static_cast from int: ";
      std::cout << "jsuit = " << jsuit << std::endl << std::endl;
    
      std::cout << "Using \"cout << 3\": " << 3 << std::endl << std::endl;
    
      std::cout << "Using \"cout << static_cast<char>(3)\": " << 
                    static_cast<char>(3) << std::endl << std::endl;
    
      std::cout << "Using char counter and static_cast to get int: " << std::endl;
      for (char ci = 3; ci <= 6; ci++) {
        std::cout << static_cast<int>(ci) << ": <" << ci << ">" << std::endl;
      }
      std::cout << std::endl << std::endl;
    
      std::cout << "Using int counter and static_cast to get char: " << std::endl;
      for (int ii = 3; ii <= 6; ii++) {
        std::cout << ii << ": <" << static_cast<char>(ii) << ">" << std::endl;
      }
    
      return 0;
    
    }
    In this case static_cast does the same thing as the old-fashioned C-style cast.

    By the way, in response to Zach L and Draco: for Windows machines, many of the non-printing ansii characters give interesting symbols that may (or may not) give something fun for you to work with. The above program prints symbols for Hearts, Diamonds, Spades, and Clubs on my Windows box (but not for my Linux box).


    [edit]
    When I said "print", I should have said "displays in a console window". Printers may or may not be able to display these characters.
    [/edit]


    Regards,

    Dave
    Last edited by Dave Evans; 08-13-2004 at 03:56 PM.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Interesting... I wouldn't have expected it to print anything. Good to know.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    60
    Code:
    #include<iostream>
    int main()
    {
    char suit[4]={3,4,5,6};
    std::cout<<suit[0]<<std::endl;
    std::cout<<suit[1]<<std::endl;
    std::cout<<suit[2]<<std::endl;
    std::cout<<suit[3]<<std::endl;
    return 0;
    }
    You don't need to use a static cast. simply do the above code or even this:
    Code:
    //#include file
    using std::cout; using std::endl; cout<<(char)3<<endl<<(char)4<<endl<<(char)5<<endl<<(char)6;
    Last edited by C++Child; 08-14-2004 at 12:07 AM.
    Child who knows C++
    Using Borland C/C++ Compiler 5.5 (Command Line Version)

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    C++Child, your second method simply uses C-style casts instead of C++ static_casts, but in this case, is essentially the same as using static_cast. In fact, the more "proper" C++ program would use static_casts there (granted, most actual programmers would stick with the C-style).

    And your main is a bit lacking...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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