Executing Files
C++ (as with any other language) allows you to execute actions on other programs from code:
ShellExecuteA(NULL, "open", "LOG_FILE.html", 0, 0, SW_SHOWNORMAL);
If you open an HTML file, it’ll open in the user’s default browser. Thus, you can write code that spits out HTML/CSS/JavaScript and open it for them. We did this with our log files and it let us add features like filtering, pagination, and more. If we took some more time to develop it, we easily could have added searching, regex and specific log filtering (turning off an instance of a log).

The function also allows you to take other actions, such as edit, find or even print! That’s right, if your player beats a level, feel free to print them out an award.
You can also execute a batch file to execute a series of commands for you. Need to compile and run a tool you wrote in another language?
python HeatMap.py
This is only really recommended for internal tools. Don’t execute code from a batch file in a released application
Unary Operators
First off, let’s get the name straight as a shocking number of people don’t know this:
- Unary Operator: Needs one variable to function. (minus sign in “-value”)
- Binary Operator: Needs two variables to function. (minus sign in “5 – 5”)
- Ternary Operator: Needs three variables to function (:?, ie “value == 5 ? 0 : 4”)
This leads us to one of the most useless operators in C++. The unary plus operator:
int value = +5;
In that case, the plus does absolutely nothing. But what does it do here?
int value1 = +false; int value2 = +true;
This converts false into a 0 and true into a 1. Maybe you have an idea of what this does at this point, but look at this code:
#include <iostream>
void Print(unsigned char let) {
std::cout << let << std::endl;
}
void Print(int num) {
std::cout << num << std::endl;
}
int main()
{
unsigned char value = 'b';
Print(value);
Print(+value);
return 0;
}
This prints:
b 98
Unary plus converts smaller types like short, char, or boolean into an int that makes sense for it. In reality, it’s there for you to overload it if you need it.
The Comma Operator
Time to go back to C++ 101. An expression is anything that results in a value after executing:
val += 5; val == true; func();
A statement is anything that doesn’t result in a value:
return 5;
if (5) {}
using namespace std;
The comma operator lets you chain an infinite number of expressions in a row:
int val = 0; val = val + 5, val + 6, val + 7; std::cout << val << std::endl;
The result of this code will be “5”. “val = val + 5” is all part of the same expression and commas have the lowest precedence. Thus, val will become 5 and then a temporary val + 6 and val + 7 will execute. In fact, we can verify this:
int val = 0;
val =
val + 5,
std::cout << val + 6 << std::endl,
std::cout << val + 7 << std::endl;
std::cout << val << std::endl;
This will print out “11” “12” and “5” in that order. Remember, each section of the commas is its own expression, so they don’t have to all return the same data type!
Thus, what does this code do?
int val = 0;
if (val += 1, val == 1) {
std::cout << "Condition was true: " << val << std::endl;
} else {
std::cout << "Condition was false: " << val << std::endl;
}
This will print “Condition was true: 1”. It will execute each expression from left to right and return the value of the right most expression.
Thus, we can also do this:
if (std::cout << "1" << std::endl, std::cout << "2" << std::endl)
std::cout << "3"<< std::endl;
And get “1”, “2”, “3”!
You can do this in any place that accepts an expression:
int main()
{
int num = 5;
return num += 1, std::cout << num << std::endl, 0;
}
This will add 1 to num, print it out and then return the proper 0 code to main. Why does this work? It’s because std::cout returns a stream! It isn’t returned to main, thus we can call whatever we want with any return type using this!
Naming
The regex for names in C++ is [A-Za-z_]+[A-Za-z0-9_]*. Basically, the first character can’t be a 0-9 and everything has to be an alphanumeric symbol or an underscore.
Well, that means we can name our variable “_”. This is valid C++ code:
struct __ {
__() {}
__* ___(__ _) {
return std::cout << "CAT" << std::endl, this;
}
};
int main()
{
__* _ = new __();
_->___(*_);
return 0;
}
Variable Naming Styles
There’s a lot of ways programmers have developed to format variable names:
- lowerCamelCase
- UpperCamelCase
- snake_case
- SCREAMING_SNAKE_CASE
- Hungarian Notation
It’s not a bad idea to create your own style of Hungarian notation:
- Private: variableName_
- Public: VariableName
- Macro: VARIABLE_NAME
- Argument: aVariableName
- Static: sVariableName
- Global: gVariableName
- Local: variableName
This can really help with tracing code you don’t know because it’s much clearer where things come from. When everything is written in lowerCamelCase, it can be very hard to read.
In fact, GoLang enforces this through the compiler. If you make a variable in lowerCamelCase, it’s private. If you name is in UpperCamelCase, it’s public. If you want a language that is great for concurrency and strict on style/warnings, GoLang is the language for you.
Rosetta Code
This is a website that is dedicated to providing solutions to hundreds of different well known programming problems in hundreds of different languages. That’s right, hundreds. They range from the common (C, Java, Python) to the uncommon (Ruby, Fortran, Haskell) to the difficult (ARM, x86) to the jokes (Chef, Brainf***, Whitespace, LOLCODE). Sometimes this is just for jokes, but sometimes it’s a great way to learn about other languages and see the available solutions!
I highly recommend 99 bottles of beer on the wall. It has some fantastic solutions.
Up next, we’re going to be talking about debugging techniques!