Function Overloading
In C++, functions with the same name can be defined multiple times with different argument types or counts. This is called function overloading. The compiler automatically selects the best-matching function based on the argument types and count at the call site, allowing related operations to be expressed under the same name.
Syntax
// ======================================== // Function overload basic syntax // ======================================== // Overload with different argument types ReturnType functionName(TypeA arg); ReturnType functionName(TypeB arg); // Overload with different number of arguments ReturnType functionName(TypeA arg1); ReturnType functionName(TypeA arg1, TypeB arg2); // Overload based on const / non-const (for member functions) ReturnType methodName(); ReturnType methodName() const; // NG: Overloads differing only in return type cause a compile error // int attack(); // Error: only return type differs — not a valid overload // void attack();
Syntax Reference
| Pattern | Description |
|---|---|
| Overload by argument type | Defines the same function name for int, double, string, etc. The matching function is selected based on the type of the argument passed by the caller. |
| Overload by argument count | Defines the same function name with one argument, two arguments, etc. Useful for providing a default behavior and a detailed variant under the same name. |
| const / non-const overload | Overloads a member function based on whether it has the const qualifier. const objects call the const version; non-const objects call the non-const version. |
| Difference from default arguments | A default argument achieves optional parameters with a single function definition; overloading has multiple definitions. Overloading is suitable when the behavior differs significantly by argument. |
| Overload by return type only (NG) | When two definitions have the same argument list and differ only in return type, they are not treated as overloads and cause a compile error. |
Sample Code
function_overload.cpp
// ========================================
// function_overload.cpp — Function overload sample
// Uses Dragon Ball characters to overload
// the attack() function in multiple ways
// ========================================
#include <iostream>
#include <string>
// Overloaded attack() definitions
// Takes a string (technique name)
void attack(const std::string& technique) {
std::cout << "Technique: " << technique << " — attack!" << std::endl;
}
// Takes an integer (power level)
void attack(int powerLevel) {
std::cout << "Power level " << powerLevel << " — attack!" << std::endl;
}
// Takes a string and an integer (technique and damage multiplier)
void attack(const std::string& technique, int multiplier) {
std::cout << "Technique: " << technique
<< " (power x" << multiplier << ") — attack!" << std::endl;
}
int main() {
// Son Goku: attack with technique name (string version selected)
std::string goku = "Son Goku";
std::cout << goku << "'s attack:" << std::endl;
attack("Kamehameha");
std::cout << std::endl;
// Vegeta: attack with power level (int version selected)
std::string vegeta = "Vegeta";
std::cout << vegeta << "'s attack:" << std::endl;
attack(18000);
std::cout << std::endl;
// Piccolo: attack with technique and multiplier (string + int version selected)
std::string piccolo = "Piccolo";
std::cout << piccolo << "'s attack:" << std::endl;
attack("Special Beam Cannon", 3);
return 0;
}
g++ -std=c++17 function_overload.cpp -o function_overload ./function_overload Son Goku's attack: Technique: Kamehameha — attack! Vegeta's attack: Power level 18000 — attack! Piccolo's attack: Technique: Special Beam Cannon (power x3) — attack!
Common Mistake 1: Defining functions that differ only in return type
Defining multiple functions with the same argument list but different return types does not create an overload and causes a compile error.
// NG: Only return type differs — not a valid overload int getPower(const std::string& name); void getPower(const std::string& name); // Compile error: same argument list
OK: Change the argument type or count to create a valid overload.
// OK: Different argument types or counts int getPowerLevel(const std::string& name); int getPowerLevel(const std::string& name, int base); void printPower(const std::string& name);
g++ -std=c++17 overload_ok.cpp -o overload_ok ./overload_ok Son Goku's power level: 9000 Vegeta's adjusted power: 10800
Common Mistake 2: Unintended overload selected due to implicit conversion
C++'s implicit type conversions may cause a different overload to be called than intended. When argument types do not match exactly, the compiler selects a function after applying conversions.
// int and double overloads
void attack(int powerLevel) {
std::cout << "int version: " << powerLevel << std::endl;
}
void attack(double powerLevel) {
std::cout << "double version: " << powerLevel << std::endl;
}
// Passing float calls the double version (float is promoted to double)
attack(9000.0f); // double version called (float → double)
attack(9000); // int version called
When the type is ambiguous, use an explicit cast to select the intended overload.
attack(static_cast<int>(9000.0f)); // Explicitly select int version attack(static_cast<double>(9000)); // Explicitly select double version
g++ -std=c++17 overload_cast.cpp -o overload_cast ./overload_cast int version: 9000 double version: 9000
Overview
Function overloading is a mechanism for defining multiple variations of a function with the same name, differing in argument types or counts. The compiler selects the best-matching definition based on the argument list at the call site. The return type is not part of this selection, so multiple definitions with the same argument list but different return types cause a compile error. The key difference from default arguments is that overloading allows multiple function bodies, making it appropriate when the behavior needs to be completely different depending on the arguments. For class member functions, overloading based on the presence or absence of const qualification allows separate behavior for read-only objects and modifiable objects.
If you find any errors or copyright issues, please contact us.