Casting Down

This is a discussion on Casting Down within the Windows Programming forums, part of the Platform Specific Boards category; Ok, so the book I used wasn't really clear on casting, but now the graphics programming book that I have ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Casting Down

    Ok, so the book I used wasn't really clear on casting, but now the graphics programming book that I have talk about it....for example the following line......

    Code:
    bool Prog_Init()
    {
    	//create the new pen
    	hpenNew=CreatePen(PS_SOLID,0,RGB(255,255,255));
    
    	//borrow dc from main window
    	HDC hdc=GetDC(hWndMain);
    
    	//select new pen into dc
    	hpenOld=(HPEN)SelectObject(hdc,hpenNew);//it says that the assignment to the hpenOld is a cast of something, 
    
    can you shed some light into this? Why is this necessary?
    
    	//release dc to system
    	ReleaseDC(hWndMain,hdc);
    
    	return(true);//return success
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Casting changes the type of data. For example:

    char blah='d';
    int blah2=(int) blah;//makes blah2 equal the ascii value of blah

    OR

    char blah='d';
    int blah2=blah;//this is an implicit cast, not preferred

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    oh cool but this doesn't always work right or does it, and why do you think it's necessary on this piece of code I showed you, any ideas?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Originally posted by elchulo2002
    oh cool but this doesn't always work right or does it, and why do you think it's necessary on this piece of code I showed you, any ideas?

    Sometimes, it doesn't work at all. Conversion operators have to be in place to convert from one type to another.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    oh ok, but do you have any ideas why this is needed here?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The win32 'handles' are of all the same basic type. They refer to something in win32 land. Pretend that handle is a synonym for void*. If you had a generic function in C that you wanted to work with different types then you could pass and return void pointers.

    However, to maintain type safety and prevent subtle bugs, void pointers should be cast to the relevant type before any assignment. This tells the compiler that you haven't forgotten what the types are, and if you attempt to assign the cast of a generic function returning a int* to a pointer to be used as a float* then the compiler can issue an error and you can fix your bug.

    Substitute HANDLE for pointer, HPEN for int* and HBRUSH for float* in the above example and you'll get the idea.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Oh thanks so the compiler wants you to specify the return value of what you are going to assign........I understand cool. Thank.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You don't always NEED to tell what type you are converting it to. However, it is preferred that you be explicit and tell what you are converting to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 04:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM

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