Solved

BOOL vs bool

Posted on 1997-10-09
3
2,464 Views
Last Modified: 2013-11-20
what is the difference between BOOL (TRUE, FALSE)
and bool (true, false). which to use ? when ? can it be mixed ?
0
Comment
Question by:has
3 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 20 total points
Comment Utility
The major difference between BOOL and bool is that one is a typedef of int (BOOL) and the other is a C++ type (bool).

You should be very careful if you mix them because the inherent sizes are different: sizeof(BOOL) == sizeof(int) == 4 and sizeof(bool) == 1. That means that if you save a BOOL into a binary stream, you may corrupt the stream if you try to read it back as a bool.

The BOOL is available as long as you include Windows.h. However, bool is available only on compilers that support the new C++ type. If you want to be as portable as possible, you should use BOOL (because there are still C++ compilers that do not support bool).

There are lots of minor problems with mixing BOOL and bool. But if you are consistent, there's no problem.
0
 

Author Comment

by:has
Comment Utility
thanks
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Also note - many MFC functions (and Win32 API functions) expect and return BOOL rather than bool.  If you are writing message handlers that return BOOL, say, don't be tempted to change them to return bool instead.  If you do then your code will break.

However, in your own code, using bool has the advantage that the compiler knows about the type, and will stop you (without a cast) assigning arbitary int values to a bool variable (like you can do with BOOL).  It also ensures the a bool only ever contains 'true' or 'false' (0 or 1).  A BOOL with any non-zero value will test as true, but if you do a comparison with TRUE, you get a fail.

So for compiler error detection, and memory size, go for bool, but always convert to/from BOOL when dealing with MFC and Win32 API.

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now