Some quick C++ references

The following are a few things that I like to keep around so I can always quickly implement similar things the correct way in C++

Summary


Pre- and post-increments

In C++ it is possible to override most of the standard C and additional C++ operators. This includes the pre- and post-increment operators (there are 4 of these).

The following shows you a template like implementation of these operators. You will notice that to distinguish between both versions, C++ uses an (int) parameter. This (int) is ignored. When the (int) is present, the function is a post-increment or post-decrement. The (void) declaration is the pre-increment or pre-decrement.

Name C++ function Comments
Post-increment
T operator ++ (void)
{
	T copy = *this;
	++*this;
	return copy;
}
We assume that the template type T has at least the pre-increment operator defined. Note that we need to copy the current value first and this means the type T needs to have a copy operator.
Pre-increment
T& operator ++ (void)
{
	return ++*this;
}
We assume that the template type T has the pre-increment operator defined.
Post-decrement
T operator -- (void)
{
	T copy = *this;
	--*this;
	return copy;
}
We assume that the template type T has at least the pre-decrement operator defined. Note that we need to copy the current value first and this means the type T needs to have a copy operator.
Pre-decrement
T& operator -- (void)
{
	return --*this;
}
We assume that the template type T has the pre-decrement operator defined.