The idea in one sentence
Function overloading means two or more functions in the same scope share a name but have different parameter lists, so the compiler can tell which one each call refers to.
Readers often ask does c have function overloading. C does not support it, while C++ does. Function overloading is one of several types of overloading, and the bigger picture is in what is overloading and its types.
A simple example
#include <iostream>
#include <string>
void print(int value) {
std::cout << "int: " << value << '\n';
}
void print(double value) {
std::cout << "double: " << value << '\n';
}
void print(const std::string& text) {
std::cout << "string: " << text << '\n';
}
int main() {
print(42); // int: 42
print(3.14); // double: 3.14
print("hello"); // string: hello
print('A'); // int: 65
}
Each call uses the version whose parameter best fits the argument. The last call is the interesting one, and the next section explains why it picks the int version.
What makes two parameter lists different
Overloads can differ in any of these ways:
- Number of parameters, such as
send(message)andsend(message, retries). - Types of parameters, such as the
intanddoubleversions ofprintabove. - Order of parameter types, such as
f(int, double)andf(double, int).
What does not count as an overload
A different return type alone isn’t enough. This won’t compile:
int get_value();
double get_value(); // error: only the return type differs
Top-level const on a parameter is also ignored. void f(int x) and void f(const int x) declare the same function. Changing only the name of a parameter doesn’t create an overload either.
How the compiler chooses a version
The compiler ranks the candidates from best to worst. An exact match is best. A promotion, such as char to int, comes next. A standard conversion, such as int to double, comes after that. A conversion made by a class constructor is the weakest.
That explains the 'A' call above. The char argument can be promoted to int, which ranks above converting it to double, so the int version wins and prints 65.
Because the choice is made at compile time, overloading is often called compile-time polymorphism. It is one part of polymorphism in C++.
Ambiguous calls and default arguments
Default arguments are a common source of errors:
void log_message(const std::string& text, int level = 0);
void log_message(const std::string& text);
log_message("start"); // error: call is ambiguous
Both versions can accept one argument, and neither is a better match, so the compiler stops. To fix this, remove one of the functions or give it a different name.
Name hiding with inheritance
When a derived class declares a member function with the same name as a base class function, the derived declaration hides all of the base class overloads with that name. This is a common surprise with inheritance in C++:
struct Base {
void print(int value);
void print(double value);
};
struct Derived : Base {
void print(const char* text); // hides both Base::print overloads
using Base::print; // brings them back into Derived
};
Without the using line, a call such as d.print(42) on a Derived object fails to compile.
Key takeaways
- Overloads share a name and differ in their parameter lists.
- The return type alone can’t tell two functions apart.
- The compiler ranks matches: exact, then promotion, then conversion.
- Ambiguous calls are compile errors, and default arguments are a frequent cause.
- Operators follow the same rules, since
operator+is a function with a special name. Operator overloading in C++ builds on this same idea.