Game Development Reference
In-Depth Information
Boolean Values
Booleans are simple values that can only contain true or false. Booleans are usually, but not always,
implemented as a single byte char. A value of 0 is usually used to represent false and any nonzero
value is true. We rarely use numbers to represent booleans, as C++ contains the true and false
keywords. A couple of boolean variable declarations would be:
bool isTrue = true;
bool isFalse = false;
We've already seen in the previous section that functions can return boolean values to help create
statements that make sense to a human reader:
bool numberIsNaN = isnan(nan);
Booleans will also become useful once we start looking at flow control statements in Chapter 5.
Enums
C++ allows us to create an enumerated list of values. This is useful if we wish to convey sets of
things that have readable names. An example of an enum in C++ is:
enum class Color
{
Red,
Green,
Blue
};
This has effectively contained a new type in C++, Color . We can create new variables of type Color
like this:
Color currentColor = Color::Red;
Using enums is a useful way to create code that is highly readable. Compare the preceding line to
this:
unsigned int currentColor = 0;
This code essentially does exactly the same as the preceding line, but it is much more difficult to
understand. We have no way of knowing that 0 was the color red and it was also possible to assign
the number 10 to currentColor , which wouldn't be a valid color.
 
Search WWH ::




Custom Search