Click to See Complete Forum and Search --> : m_bChech == true results in warning!


Shahzad
April 11th, 1999, 07:24 AM
I am using the if expression with boolean data types. The strang thing is that comparison with false does not give error but the same with true gives error.

if (m_bCheck == false); // ok
if (m_bCheck !=true); // ok
if (m_bCheck == true); // Here it gives warning message related to == operator.
// says one side is const bool and other is somthing else...

Please help with this!

LALeonard
April 11th, 1999, 01:42 PM
I bet m_bCheck is declared as BOOL instead of bool. BOOL is an int. bool is a bool. Don't mix the two.

Next time, post the warning message, like "warning C4805: '==' : unsafe mix of type 'int' and type 'const bool' in operation".)


LA Leonard - Definitive Solutions, Inc.

Shahzad
April 12th, 1999, 12:30 AM
Thankx for pointing out the error so precisely.
I understand both BOOL & bool, takes the values false and true, so is the difference only in return type?

"BOOL Boolean variable (should be TRUE or FALSE" // from vc help

Is there any kind of difference between true & TRUE, and similarly between false & FALSE.

Dave Lorde
April 12th, 1999, 04:40 AM
As LA Leonard said, BOOL and bool are different types. TRUE and FALSE are #defines for int values (generally 1 and 0 respectively). true and false are the two permitted values of type bool.

bool has conversions to int, but if you want to convert a BOOL to a bool (e.g. a BOOL return value from a Win32 API), you can do something clunky like:

bool ret = BOOL_Func() != FALSE;

or even:

bool ret = !!BOOL_Func();

Wrap the conversion in a utility function, and you can select how you do it without upsetting things:

bool to_bool(BOOL b) { return FALSE != b; }
bool ret = to_bool(BOOL_Func());

Dave