August 2012
Intermediate to advanced
976 pages
30h 17m
English
bool LiteralsIf we want to test the truth value of an arithmetic or pointer object, the most direct way is to use the value as a condition:
if (val) { /* ... */ } // true if val is any nonzero valueif (!val) { /* ... */ } // true if val is zero
In both conditions, the compiler converts val to bool. The first condition succeeds so long as val is nonzero; the second succeeds if val is zero.
We might think we could rewrite a test of this kind as
if (val == true) { /* ... */ } // true only if val is equal to 1!
There are two problems with this approach. First, it is longer and less direct than the previous code (although admittedly when first learning C++ this kind of abbreviation can be perplexing). Much more importantly, ...
Read now
Unlock full access