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

Updated 18 Nov 20266 min read
Function and Operator Overloading in C\u002b\u002b: Compile-Time Polymorphism with Worked Resolution Traces

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:

cpp
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) selects area(int), so 6 * 6 = 36.

  • area(8, 5) selects area(int, int), so 8 * 5 = 40.

  • area(2.5) selects area(double), so 3.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:

cpp
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 an int argument. The exact int match returns 10.

  • With signed char code = 65, pick(code) promotes code to int and returns 10.

  • pick(2.5f) promotes the float argument to double and returns 20.

  • pick(7L) has a long argument. The exact long match returns 30.

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:

cpp
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.

Diagram showing C++ overload resolution for exact matches, integral and floating-point promotions, a long match, and an ambiguous mix(5, 5) call.

Operator overloading in C++: add two complex numbers step by step

A class can give + an addition-like meaning:

cpp
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.

Diagram tracing z1 = 3 + 4i and z2 = -1 + 5i through operator+ to real sum 2, imaginary sum 9, and Complex {2, 9}.

Operator overloading rules: what C++ lets you change

Three invariants apply:

  1. 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.

  2. An overload cannot invent a new operator symbol.

  3. 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:

cpp
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

int convert(int) and double convert(int)

Return type alone cannot select a call

Change the parameter list or function name

log(int) and log(int, int level = 0), called as log(5)

The default makes both candidates viable

Remove the overlapping default or rename the operation

mix(5, 5) with the crossed overloads

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:

  1. Is the overload set legal? Check parameter lists first. Return-type-only differences are illegal.

  2. Which conversion wins? For signed char code = 65, pick(code) uses integral promotion to int and returns 10. For pick(2.5f), float promotes to double, so the result is 20.

  3. Is the call ambiguous? For mix(5, 5), yes. Neither crossed candidate is better for both arguments.

  4. How many explicit parameters are present? A member binary operator has one, while a non-member binary operator has two.

  5. 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:

cpp
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.