Function and Operator Overloading in C\u002b\u002b: Compile-Time Polymorphism with Worked Resolution Traces
See how C++ chooses an overloaded function, why some calls are ambiguous, and how member operators work through exact, calculated examples.
KnowledgeGate Team
Exam prep & CS education

A function name or operator symbol can trigger different code in C++, but that does not automatically make it runtime polymorphism. The key question is what the compiler can decide from the argument types before the program runs. Function overloading, operator overloading, and overriding become clearer when actual calls and values are traced.
The earlier C++ Function and Operator Overloading MCQs: 12 Solved Questions owns the exam-style question drill; runnable overload traces, operator implementations, and calculated outputs take priority here.
Function and operator overloading: what compile-time polymorphism means
Compile-time polymorphism, also called static polymorphism, means that the compiler selects one declaration from a valid set during compilation. Function overloading reuses a function name with different parameter lists. Operator overloading gives an existing operator a class-specific meaning.
Think of add(7, 4), add(2.5, 1.5), and z1 + z2. The first two calls can select different add declarations, while the last can call an overloaded operator+. Selection depends on declared types and available signatures, not on the eventual result. Explore the broader language route under Coding & DSA.
Function overloading in C++: build one valid overload set
Here is a valid overload set for area:
int area(int side) { return side * side; }
int area(int length, int breadth) { return length * breadth; }
double area(double radius) { return 3.14 * radius * radius; }Each call has a clear target:
area(6)selectsarea(int), so6 * 6 = 36.area(8, 5)selectsarea(int, int), so8 * 5 = 40.area(2.5)selectsarea(double), so3.14 * 2.5 * 2.5 = 3.14 * 6.25 = 19.625.
Overloads may differ by parameter count, types, or order. They cannot differ only by return type. int convert(int) and double convert(int) cannot coexist because convert(5) provides no basis for choosing one.
For declaration visibility and default-argument ambiguity, read C++ Overloading and Default Arguments: Rules and Examples, which owns that mechanism. C++ Function Overloading and Default Arguments: Resolve Valid and Ambiguous Calls isolates the multi-call ranking workflow; the worked examples below apply selected-signature rules to user-defined operators and computed traces.
C++ overload resolution: exact match, promotion, conversion, and ambiguity
Consider these declarations:
int pick(int) { return 10; }
int pick(double) { return 20; }
int pick(long) { return 30; }For this set, the short working ladder is exact match before promotion, then standard conversion:
pick(7)has anintargument. The exactintmatch returns10.With
signed char code = 65,pick(code)promotescodetointand returns10.pick(2.5f)promotes thefloatargument todoubleand returns20.pick(7L)has alongargument. The exactlongmatch returns30.
This short ladder does not replace C++'s further rules for references, templates, and user-defined conversions.
Ambiguity appears when no candidate is better for every argument:
int mix(int, double) { return 1; }
int mix(double, int) { return 2; }For mix(5, 5), the first candidate is exact on argument 1 but converts argument 2. The second converts argument 1 but is exact on argument 2. Neither is better overall, so compilation fails as ambiguous. Neither 1 nor 2 is returned.

Operator overloading in C++: add two complex numbers step by step
A class can give + an addition-like meaning:
struct Complex {
int real;
int imag;
Complex operator+(const Complex& rhs) const {
return {real + rhs.real, imag + rhs.imag};
}
};
Complex z1{3, 4};
Complex z2{-1, 5};
Complex sum = z1 + z2;Here, z1 is 3 + 4i and z2 is -1 + 5i. The real part is 3 + (-1) = 2, and the imaginary part is 4 + 5 = 9. The returned {2, 9} represents 2 + 9i.
The equivalent member-call view is z1.operator+(z2). The object z1 is the implicit left operand, and z2 is the single explicit argument.

Operator overloading rules: what C++ lets you change
Three invariants apply:
For ordinary overloaded operators, at least one operand must have a user-defined class or enumeration type; allocation and deallocation functions (
operator new,operator delete, and their array forms) are exceptions.An overload cannot invent a new operator symbol.
An overload cannot change an operator's precedence, associativity, or number of operands.
::, ., .*, ?:, and sizeof cannot be overloaded.
For binary +, z1.operator+(z2) has one explicit parameter, while non-member operator+(z1, z2) has two. Stream insertion is commonly a non-member so std::ostream remains the left operand. Good design keeps + addition-like, though the compiler cannot enforce that choice.
Prefix and postfix increment are distinguished by their signatures:
struct Counter {
int value;
Counter& operator++() { ++value; return *this; }
Counter operator++(int) {
Counter old = *this;
++value;
return old;
}
};With Counter c{7}, prefix ++c changes the value to 8 before returning. A following postfix c++ returns the old snapshot 8, then leaves the value at 9. The int parameter is a dummy signature marker, not the increment amount.
Function and operator overloading traps: diagnose and repair them
Trap | Why it fails | Repair |
|---|---|---|
| Return type alone cannot select a call | Change the parameter list or function name |
| The default makes both candidates viable | Remove the overlapping default or rename the operation |
| Each candidate has one exact match and one conversion | Cast one argument or redesign the overload set |
Overloading uses the same name with different signatures and compile-time selection. Overriding replaces a virtual base-class operation with a compatible derived implementation, selected through runtime dispatch. Virtual Functions and VTable in C++: Runtime Dispatch develops that contrast.
How exams test function and operator overloading
Use five checks to solve common conceptual questions:
Is the overload set legal? Check parameter lists first. Return-type-only differences are illegal.
Which conversion wins? For
signed char code = 65,pick(code)uses integral promotion tointand returns10. Forpick(2.5f),floatpromotes todouble, so the result is20.Is the call ambiguous? For
mix(5, 5), yes. Neither crossed candidate is better for both arguments.How many explicit parameters are present? A member binary operator has one, while a non-member binary operator has two.
Is selection static or dynamic? Overload resolution uses compile-time call information. Virtual overriding uses runtime dispatch.
Now trace both increment forms with Counter c{7}. For the Counter definition above, the type-correct value capture is:
int a = (++c).value;
int b = (c++).value;Prefix changes c.value from 7 to 8, so a = 8. Postfix copies 8, changes the stored value to 9, and gives the snapshot to b. Therefore b = 8 and final c.value = 9.
A shorthand such as int a = ++c; int b = c++; needs a separate conversion from Counter to int; the declared class does not provide one. That conversion detail does not change the increment trace: postfix returns the pre-increment snapshot, and each operation mutates the object once.
Function and operator overloading: the short version and next step
Parameter signatures form a function overload set.
Return type alone is not enough to distinguish overloads.
In the worked ladder, exact match outranks promotion, which outranks standard conversion.
Operator overloading does not change precedence, associativity, or operand count.
Virtual overriding is runtime polymorphism, not overloading.
If you want a sequenced treatment of these rules with more language practice, continue with the C++ Programming course. Focus on predicting the selected signature before running the code. That habit turns overload resolution from guesswork into a repeatable process.
Keep learning
C++ Function and Operator Overloading MCQs: 12 Solved Questions
Solve 12 bank-backed C++ MCQs on overload resolution, operator rules, member signatures, conversion functions, polymorphism, and expression tracing.
C++ Function Overloading and Default Arguments: Resolve Valid and Ambiguous Calls
Learn why default arguments affect viability but not ranking, then resolve five score calls using exact matches, promotions and conversions. Includes ambiguity repairs and call-time default evaluation.
Polymorphism in Java: Overloading, Overriding and Runnable Examples
Learn how Java chooses overloaded methods at compile time and overridden methods at runtime. Trace two complete programs, common traps and practice targets.
C++ const, constexpr and consteval: Runtime Immutability vs Compile-Time Guarantees
See exactly what each C++ keyword guarantees. One C++20 program separates immutability, optional constant evaluation and mandatory constant evaluation.





