Function Overloading in C++
Overloading lets several functions share one name as long as their parameter lists differ — print(int), print(double), print(string) are three distinct functions the compiler tells apart at each call. It expresses one idea (print, max, construct) across types without inventing printInt/printDouble clutter.
Resolution picks the best viable match, preferring exact types over promotions over conversions; ambiguous calls (equally good matches) fail to compile rather than guess. Return type alone never distinguishes overloads — parameters must differ in count, type, or order.
Same name, different params
Overloads differ by arity or parameter types. The compiler mangles each into a unique internal symbol behind the scenes.
Best match wins
Exact type beats promotion (short to int) beats conversion (int to double). The ranking is deterministic, not heuristic.
Ambiguity is an error
Two equally good candidates make the call ambiguous — the compiler rejects it instead of picking arbitrarily.
Return type doesn't count
int f() vs double f() with identical parameters is a redefinition error — callers can't always use the result, so it can't disambiguate.
#include <iostream> int max2(int a, int b) { return (a > b) ? a : b; }double max2(double a, double b) { return (a > b) ? a : b; } int main() { std::cout << max2(3, 7) << std::endl; std::cout << max2(3.5, 2.5) << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.