Function overloading
Somewhat similar to templates, function overloading is another way in which we can make code and classes more versatile. We've already used overloaded functions during the course of the book, but they were provided with the code base. So, let's take a quick look at them.
When we define functions, we set fixed parameter types. Here's an example:
void DoStuff(T parameter);
With this definition, we can only pass a parameter of the T
type. What if we want a choice of parameters? What if we want to be able to pass parameters of type T
or type Y
. Well, we can redefine the function, setting the same return type and name, but with unique parameters:
void DoStuff(T parameter); void DoStuff(Y parameter);
We now have two function declarations with different parameters. When we call DoStuff
, we'll have the option of which parameter to pass. Also, with function overloading, each declaration gets its own body, just like with template specialization. While similar on the surface, function...