[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

BOOL vs bool

Posted on 1997-10-09
3
Medium Priority
?
2,586 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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Loops Section Overview
Suggested Courses

834 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