Problem comparing bool values

I have something along the lines of

1
2
3
4
5
6
Class A
{
    bool foo;
    public:
    bool getFoo(){return foo;}
}


and then elsewhere
1
2
3
4
5
6
7
8
void compare(A &a, A &b)
{
   std::cout<<a.getFoo()<<":"<<b.getFoo()<<"\n";
   if(a.getFoo()==b.getFoo())
       std::cout<<"equal";
   else
       std::cout<<"not equal";
}


No matter the values of foo the output is always

204:204
not equal



SOLVED:
204 was caused by trying to use an empty bool
Last edited on
What compiler/operating system are you using?

It works on Coliru:
http://coliru.stacked-crooked.com/a/cd73bac4002c5f1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class A
{
    bool foo;
    public:
        A(bool a): foo(a) {}
        bool getFoo(){return foo;}
};

void compare(A &a, A &b)
{
   std::cout<<a.getFoo()<<":"<<b.getFoo()<<"\n";
   if(a.getFoo()==b.getFoo())
       std::cout<<"equal";
   else
       std::cout<<"not equal";
}

int main()
{
    A a1(true), a2(true);
    compare(a1, a2);
}
1:1
equal
Last edited on
Tried compiling my simple example in a different project and it worked. Spent awhile trying to trace the problem back and it turns out in one of the constructors for my class I didn't assign a value to the bool.

Basically I am dumb. =P
Last edited on
But that doesn't explain why the value printed is 204 rather than 1 or 0. Which compiler are you using?
Hmm...

Could an uninitialised bool have the same effect as any other uninitialised variable?

The OP didn't seem to provide a constructor (but long double main did), so an implicit one would be used, aren't the member variables initialised to some default value hence the purpose of using initialiser lists?

Maybe it is just UB?
I tried getting rid of the constructor and just using the implicit one, and with -O3 on MinGW GCC 4.8.1, I got an output of
34:0
not equal

Either way, I'm not sure how to explain the fact that the OP got 204 for both of them and it still came out to be not equal.
So UB then?

If it is, then I guess we could spend all day trying explain UB and not get anywhere :+)

Any way what displaying them with boolalpha? I am thinking it is some thing perverse with the way cout displays them versus the actual implementation and / or that they are separate objects.

Sorry if I sound lazy, but you have the code :+)

Probably wasting out time if it is UB, so only if you are bored.
> No matter the values of foo the output is always
> 204:204
> not equal

>> But that doesn't explain why the value printed is 204 rather than 1 or 0.

>>> I'm not sure how to explain the fact that the OP got 204 for both of them
>>> and it still came out to be not equal.

>>>> I am thinking it is some thing perverse with the way cout displays them

Footnote: Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false. - IS
Yay! JLBorges to the rescue again :+D

Cheers. Will have to get around to reading the standard one day. Or at least learn how to find things in it !
There is not much point in reading the IS like a text book.

Learning how to find things in it is useful; to get a doubt clarified, or to check if there really is a bug that ought to be reported to the compiler vendor.
Topic archived. No new replies allowed.