Very simple, what does this mean?

This is a discussion on Very simple, what does this mean? within the C++ Programming forums, part of the General Programming Boards category; This is a very simple question, what does the following mean Code: windowClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); More specifically what is the ...

  1. #1
    Shadow12345
    Guest

    Very simple, what does this mean?

    This is a very simple question, what does the following mean
    Code:
    windowClass.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH));
    More specifically what is the (HBRUSH) doing in front of the GetStockObject function call? I mean come on, that is just silly!

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    It is casting the GetStockObject(WHITE_BRUSH) to a HBRUSH so that windowClass.hbrBackground recives it as a HBRUSH type.
    Last edited by Barjor; 08-02-2002 at 02:00 PM.

  3. #3
    Shadow12345
    Guest
    Ok, I thought it was some type of cast. However, what type would hbrBackground read it as if I removed the (HBRUSH)? Shouldn't the function be declared to return a type of HBRUSH?
    Like I said, this is very silly

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what does the following mean
    It sets the background color of the window to white.

    >More specifically what is the (HBRUSH) doing in front of the GetStockObject function call?
    Um...it's a cast.

    >I mean come on, that is just silly!
    Try taking it out and see what happens. Half the fun of programming is fiddling with code to see how it works and why.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    casting can usually be done it 2 simple ways:

    int num = 'a'

    char( num ); or
    (char)num;

    you could see either method, I prefer the second one
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    well..the way msdn defines it is
    <<Return Values

    If the function succeeds, the return value identifies the logical object requested. NULL indicates failure. To get extended error information, call GetLastError.

    <<

    To me that means that it should return a HBRUSH..

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you know, HBRUSH is a void * as well as every other handle in windows. GetStockObject returns a handle which is a void *. But yes, casting is still nice because it shows exactly what type of handle

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    But yes, casting is still nice because it shows exactly what type of handle
    A cast is never nice and should be avoided. The segment could be written like this:
    Code:
    #include <windowsx.h>
    
    // ...
    
    windowClass.hbrBackground = GetStockBrush(WHITE_BRUSH));
    - lmov

  9. #9
    Shadow12345
    Guest
    holy crap yeah wow i didn't expect this discussion to get this big.
    its a cast its a cast i get it

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by lmov
    A cast is never nice and should be avoided.
    What are you serious?? Do you mean in this situation or ever! You're insane if you think you should never use casts. Don't get these people mixed up in the beginning by telling them to never use casts.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Originally posted by lmov
    A cast is never nice and should be avoided. The segment could be written like this:
    Code:
    #include <windowsx.h>
    
    // ...
    
    windowClass.hbrBackground = GetStockBrush(WHITE_BRUSH));
    umm there is no GetStockBrush function in the win32 api.
    GetStockObject is what they want you to use

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    umm there is no GetStockBrush function in the win32 api.
    GetStockObject is what they want you to use
    While there is no GetStockBrush function, there is the GetStockBrush macro in <windowsx.h>. See question 4.2 at http://winprog.org/faq/.
    What are you serious?? Do you mean in this situation or ever! You're insane if you think you should never use casts. Don't get these people mixed up in the beginning by telling them to never use casts.
    I did not say that casts should never be used, I said that they are not nice and should be avoided. But if you believe that casts are so essential, show me what problem cannot be solved without one (I can only think of casting the result of malloc, which is easily taken care of with a wrapper).
    - lmov

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by lmov
    While there is no GetStockBrush function, there is the GetStockBrush macro in <windowsx.h>. See question 4.2 at http://winprog.org/faq/.
    I did not say that casts should never be used, I said that they are not nice and should be avoided. But if you believe that casts are so essential, show me what problem cannot be solved without one (I can only think of casting the result of malloc, which is easily taken care of with a wrapper).
    dynamic_cast comes to mind. I suppose you can do it a different way but dynamic_casts ... to me... are convienent. Or, how about you have a 32bit float and want to check individual bits you could cast the address to a pointer to an integer and manipulate bits as if it were a integer. Those just came to mind but I'm sure there are other ways, I have always casted for those two. I was trying to sound negative or anything (even though I did) I just didn't want someone who didn't know what they were talking about (not necessarily you) to give people incorrect information without knowing the facts themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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