I ran across some very interesting code that makes me wonder about what bool is. I’ve always considered it to be a primitive type, like int or char or long. But today, I saw something that looked like this:
void boolPtrTest()
{
bool thisBool = true;
boolPtrHere(thisBool);
printf("thisBool is %s\n", thisBool ? "true" : "false");
}
void boolPtrHere(bool& theBool)
{
theBool = false; // uhh, dereferencing anyone?
}
And this code runs – no errors – and prints “thisBool is false”!
To further make this odd, I ran the following code:
bool myBool = new bool();
…and the code ran fine!
Before you go and downvote me for asking a “noobish” question
Here’s my question: what is bool? Is it defined on an implementation-by-implementation basis? From the evidence shown above, I would say that it’s a class. From a practical standpoint (disregarding the above), it would also seem proper to define a bool as a typedef to an int / char or have it #define’d. But how does one know what it is, (which would affect how you would treat it)?
EDIT: I thought I’d add that I’m working in VS 2008.
There is nothing wrong with this code. The bool is taken by reference. No dereferencing is required.
newreturns an address, which is converted totrue, since it never returns a nonzero value. This is a common conversion, especially in C code: