Solved

BOOL vs bool

Posted on 1997-10-09
3
2,518 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
ID: 1307871
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
ID: 1307872
thanks
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1307873
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

820 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