C++ Function Overloading
In C++ several functions can share the same name, as long as their parameter lists differ. This is called function overloading. The compiler looks at the arguments of each call and picks the best-matching version — a process called overload resolution.
Why overload?
Without overloading you would need names like printInt, printDouble and printString. With it, one natural name works for all of them:
#include <iostream>
#include <string>
using namespace std;
void show(int value) {
cout << "int: " << value << "\n";
}
void show(double value) {
cout << "double: " << value << "\n";
}
void show(const string& value) {
cout << "string: \"" << value << "\"\n";
}
void show(bool value) {
cout << "bool: " << boolalpha << value << "\n";
}
int main() {
show(42);
show(3.14);
show(string("hi"));
show(true);
}int: 42
double: 3.14
string: "hi"
bool: true
Different numbers of parameters
Overloads can also differ in how many parameters they take:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
int main() {
cout << add(2, 3) << "\n";
cout << add(2, 3, 4) << "\n";
cout << add(2.5, 0.25) << "\n";
}5
9
2.75
What counts as "different"?
| Overloads allowed? | Example |
|---|---|
| Yes — different parameter types | f(int) and f(double) |
| Yes — different number of parameters | f(int) and f(int, int) |
Yes — const reference vs. non-const reference |
f(string&) and f(const string&) |
| No — only the return type differs | int f(int) and double f(int) |
| No — only parameter names differ | f(int a) and f(int b) |
The return type is not part of overload resolution, because at the call site f(5); the compiler would have no way to choose.
int parse(const string& s);
double parse(const string& s); // ERROR: differs only in return typeAmbiguous calls
Sometimes no single overload is the best match, and the compiler reports an ambiguity error:
void area(int side);
void area(double radius);
area(5); // OK: exact match int
area(5.0); // OK: exact match double
area(5.0f); // OK: float -> double is a promotion
area(5L); // ERROR: long -> int and long -> double are equally goodFix it by passing the right type (area(5)) or casting (area(static_cast<int>(x))).
Overloading with structs
Overloading shines when the same operation makes sense for several of your own types:
#include <iostream>
#include <cmath>
#include <numbers>
using namespace std;
struct Circle { double r; };
struct Rectangle { double w, h; };
struct Triangle { double base, height; };
double area(const Circle& c) { return numbers::pi * c.r * c.r; }
double area(const Rectangle& r) { return r.w * r.h; }
double area(const Triangle& t) { return 0.5 * t.base * t.height; }
int main() {
cout << area(Rectangle{3, 4}) << "\n";
cout << area(Triangle{6, 5}) << "\n";
cout << round(area(Circle{1}) * 100) / 100 << "\n";
}12
15
3.14
Tip: When overloads do the same thing for different types, a template (covered later) can often replace them with a single definition. Use overloading when the implementations really differ.
Test yourself
Which pair of functions can NOT be overloaded?
- void f(int) / void f(double)
- void f(int) / void f(int, int)
- int f(int) / double f(int)
- void f(char) / void f(string)
Given overloads g(int) and g(double), which one does g(7) call?
- g(int)
- g(double)
- Both
- Ambiguous
What is the process of choosing the right overload called?
- Name mangling
- Overload resolution
- Type erasure
- Linking