BOOL to bool conversion
-
Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type
bool
and has decided to continue using it when developing MFC apps. From MSDN, I understood thatbool
type variable would occupy less memory thanBOOL
(1 bytes vs. 2 or 4 bytes), sinceBOOL
is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?
BOOL bTest; if (bParameter == true) bTest = TRUE; else bTest = FALSE;
Thanks!
-
Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type
bool
and has decided to continue using it when developing MFC apps. From MSDN, I understood thatbool
type variable would occupy less memory thanBOOL
(1 bytes vs. 2 or 4 bytes), sinceBOOL
is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?
BOOL bTest; if (bParameter == true) bTest = TRUE; else bTest = FALSE;
Thanks!
-
Hi guys, as coming from a pure C++ programming background, I'm quite used to the use of boolean type
bool
and has decided to continue using it when developing MFC apps. From MSDN, I understood thatbool
type variable would occupy less memory thanBOOL
(1 bytes vs. 2 or 4 bytes), sinceBOOL
is actually int type. Correct? In my programs, I have some BOOL type DDX control value to be passed as function parameters. And when I did that, I get warning message like the following:warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
performance warning, doesn't sound very good to me :( Is it better I do a conversion manually before passing it or that effort isn't generally required?
BOOL bTest; if (bParameter == true) bTest = TRUE; else bTest = FALSE;
Thanks!
You have it backwards, the performance hit is when you do a BOOL to a bool. bool bOut = bIn != FALSE; BTW, with a BOOL, never do (x == TRUE) as a test. As far as conditionals go, it is 0 or not 0. It doesn't test for FALSE and TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
You have it backwards, the performance hit is when you do a BOOL to a bool. bool bOut = bIn != FALSE; BTW, with a BOOL, never do (x == TRUE) as a test. As far as conditionals go, it is 0 or not 0. It doesn't test for FALSE and TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Thanks alot guys, the comments are very useful. Tim, are you saying a BOOL type variable can be more than just TRUE (1) and FALSE (0) ?
-
actually, yes. But I've not seen any app setting the value of a BOOL to anything other apart from TRUE and FALSE. TRUE is just a #define (1), while FALSE is a #define (0)
-
Thanks alot guys, the comments are very useful. Tim, are you saying a BOOL type variable can be more than just TRUE (1) and FALSE (0) ?
if your BOOL > 0 it is TRUE. if your BOOL = 0 it is FALSE. Don't know for sure for values < 0. their are apps eg. where your BOOL has value 2 or 3 , ... then you can do
if(YOURBOOL){ ... }
personally I think BOOL is against the whole idea of the basic datatype of a boolean. I would recommend a bool if you want to use true or false and an int or something if you have more values (and doif(yourint == 3){...}
) but it's MFC :~ "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix -
Don;t be too sure about it...
TrackPopupMenu
return BOOL and it can be made to return which menu item was selected in the popup menu (setting via parameters). -
if your BOOL > 0 it is TRUE. if your BOOL = 0 it is FALSE. Don't know for sure for values < 0. their are apps eg. where your BOOL has value 2 or 3 , ... then you can do
if(YOURBOOL){ ... }
personally I think BOOL is against the whole idea of the basic datatype of a boolean. I would recommend a bool if you want to use true or false and an int or something if you have more values (and doif(yourint == 3){...}
) but it's MFC :~ "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix -
Thanks; that's why I said "I haven't seen any app....". Now I know there IS at least one API doing such things :) regards
The WIN32 API is a bit of a minefield when it comes to BOOL processing. On NT the API might return a true TRUE/FALSE while on Win9x, then API might return ==0/!=0. This is why I always say to play it safe and never test for "== TRUE". Tim Smith I'm going to patent thought. I have yet to see any prior art.