Archived

This topic is now archived and is closed to further replies.

To use or not to use bool?

This topic is 5889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This has been on my mind recently. I love using bool, bool variables and bool functions (so I get to return true instead of 1). Is there a reason not to? Do bool''s have a higher performance hit then an int (doubting, but it''s better to know for sure)? Also, I do recall something about bool being something microsoft came up with (it''s been a long time, I dont know how true this is) but if that''s the case, it''s not an issue. I have no intention of ever making any of my program''s multi-platform so it all works out If you can read this, All your base are belong to us!

Share this post


Link to post
Share on other sites
Well, as far as I know bool is part of the C++ standard.
It depends on how you declare it, there is BOOL and then there is bool. BOOL is defined in VC++ and is not part of any standard.
The only drawback to using bool that I know of is no specific error code if a function fails.

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net
------------------------------
"Evil attacks from all sides
but the greatest evil attacks
from within." Me
------------------------------

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
bool is just a nice way to look at the int version of true/false states. I like to keep using just ints, then can define TRUE = 1, FALSE = 0. That way can do things like multiply the state, whereas it does not make sense to multiply bool * int.

Share this post


Link to post
Share on other sites
quote:
Original post by griffenjam
Well, as far as I know bool is part of the C++ standard.


The C standard as well (since 1999), although many C compilers haven''t been updated yet (like MSVC''s C compiler, it won''t let you use bool).

Also, a bool isn''t always one byte. It is normally, but the absolute standard doesn''t set a size in bytes for any type.

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
quote:
Original post by griffenjam
BOOL is defined in VC++ and is not part of any standard.


BOOL isn''t a VC++ specific type. It''s a typedef in the windows headers. The reason for it is that at the time the Windows headers where written (for the first time) the bool-type wasn''t in the standard.

Share this post


Link to post
Share on other sites
I remember reading that the size of a bool was to be one byte - so it''s _bad to use bool''s on a 32bit machine. It''ll have less trouble with BOOLs (since they''re DWORDs). 99.99999% of the time it probably wouldn''t matter, but BOOLs are ever-so slightly faster.

They were mostly added so that you could overload comparative operators properly (==, !=, <, etc...).

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
bool is just a nice way to look at the int version of true/false states. I like to keep using just ints, then can define TRUE = 1, FALSE = 0. That way can do things like multiply the state, whereas it does not make sense to multiply bool * int.

It doesnt make sense to multiply booleans at all, whether represented by an int or a bool. You get the exact same result by using logical AND on the booleans.



"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
Also, a bool isn't always one byte. It is normally, but the absolute standard doesn't set a size in bytes for any type.



Actually, it does. The C Standard formally specifies that the char, unsigned char, and signed char types are exactly 1 byte long. But you're right about bool, of course.

Edited by - spock on October 24, 2001 11:14:03 AM

Share this post


Link to post
Share on other sites
i think you should use int instead of bool for return types of funcntions. Lets say that you have a function that does all the direct x initialization. Lets say one thing doesnt work so you return false. Now you have no idea what part of the initilization failed. You know that just part of it failed somewhere. It would be nicer to know what part. With ints you can go this. If it failes at the first part then return 1, second part then return 2.........no failures then return 0;
This way if your game fails then you can tell the client why. You can tell them that "You dont have a hardware accelerated 3D card" or "Your card doesnt support that resolution" instead of just saying "Initialization failure"


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
quote:
Original post by ncsu121978
...This way if your game fails then you can tell the client why. You can tell them that "You dont have a hardware accelerated 3D card" or "Your card doesnt support that resolution" instead of just saying "Initialization failure"

This is why C++ has exceptions. Use them.

#include <exception>
//
void Initialize_Graphics(void)
{
HRESULT hr = Direct3D8Create(...);
if FAILED(hr)
{
throw new exception("Failed to create DirectGraphics object.");
}
...
}
//
// in your WinMain()...
try
{
Initialize_Graphics();
// other init functions
}
catch(exception &e) // catch all errors from preceding try block
{
MessageBox(..., e.what(), ...);
}

Share this post


Link to post
Share on other sites
Bah, I'm so stubborn and set in my ways I refuse to switch over to new types.

BOOL? No, bool!
DWORD? No, ulong! (typedef unsigned long ulong)
LPSZx? No! char*!

I should probably NOT do that for my OpenGL library too, but I still do. Portability isn't one of my strongpoints.

Edited by - Omaha on October 28, 2001 1:56:13 AM

Share this post


Link to post
Share on other sites