Overloading a function means that you can create different functions with the same name, as long as their signatures are different. That means that the parameter list must be of a different number and/or data types from one to another.
Overloading functions can make reading code much clearer/easier. This way functions that perform the same task all have the same name.
Note: A function cannot be overloaded based upon return type alone. It must have different parameters.
Note: Sometimes the compiler cannot tell which function it should call. This is called an ambiguous invocation, and will cause a compiler error. This often happens because C++ will try to covert numbers into a larger type if possible.
Below is an example of an overloaded function:
int add(int x, int y)
{
return x + y;
}
int add(int x, int y, int z)
{
return add(add(x, y), z);
}
double add(double x, double y)
{
return x + y;
}
Overloading a Function was originally found on Access 2 Learn
One Comment
Comments are closed.