Daily C++ Puzzle 6

The overload that hid the other one

A short C++ program with a bug: the overload that hid the other one. The answer, the observed output, the fix, and the lesson are on this page.

#include <iostream>

struct Base {
    void f(double x) { std::cout << "base double: " << x << '\n'; }
};
struct Derived : Base {
    void f(int x) { std::cout << "derived int: " << x << '\n'; }
};

int main() {
    Derived d;
    d.f(3.5);
}

A derived-class member hides every base-class member of the same name, regardless of signature — overload resolution never sees Base::f. So 3.5 converts to int and the derived version runs.

Observed output: derived int: 3.

Fix: using Base::f; inside Derived to bring the base overloads back into scope.

Lesson: hiding is by name, not by signature.

Facts verified 2026-11-04. The code on this page was compiled and run on the box with GCC 16.2.0 at -std=c++23 -Wall -Wextra; the outputs quoted are the observed outputs. MSVC was not tested.

Back to the daily tips index

Keep Exploring

Keep exploring the docs, samples, and free tools to continue your evaluation.