Switch statements work with basically anything that has an integer equivalent. Enumerations, const values, etc.
Ternary operators only work on booleans, but those booleans can be calculated at runtime. Like, assume we have an EVeggieType
enum, with two possible values: “Potato” and “Tomato”. Then let’s say we’ve got an instance of that enum called ThisVeggie
:
FString VegetableName = (ThisVeggie == EVeggieType::Potato) ? TEXT("Potato") : TEXT("Tomato");
Because ThisVeggie == EVeggieType::Potato
will also resolve as a boolean value, you can use it in a ternary operator.
It’s worth noting that in C or C++, =
is an assignment, while ==
is for testing equality. So this would assign the value Potato
to ThisVeggie
:
ThisVeggie = EVeggieType::Potato
While this would be a boolean value of either true
or false
based on whether or not ThisVeggie
already is a potato:
ThisVeggie == EVeggieType::Potato
One of the most common careless mistakes when starting out is to make a typo and miss that second equals sign. Thankfully, most compilers will catch that and warn you, but some folks prefer to flip the equality test:
EVeggieType::Potato == ThisVeggie
That will return the same true
or false
value as previously – both sides are equal! – but if you made a typo and omitted the equals sign:
EVeggieType::Potato = ThisVeggie
…will not compile, because you cannot assign a new value to what “potato” actually is.
This does, however, have the downside of making code feel like it was written by Yoda when experienced programmers read it. (“A potato, this veggie is. Mm, yes.”)
If you’re diving into C++, I highly recommend picking up a couple of books to start out in it. I’m told that Packt Publishing’s “Beginning C++ Game Programming” is not a bad one, as I guess all the examples are towards building a simple 2D game. But having never flipped through it, I’ve got no personal opinion one way or the other.