skip to content

In C++, why does a derived class that declares scale(int) make the base class's scale(double) overloads uncallable through a derived object?

level: middleimportance: must knowfreq 58%

answer

  1. lookup before overload resolution
  2. first scope with the name wins
  3. the whole overload set is hidden
  4. a using-declaration in the class

basics

~20 s

Name lookup stops at the first scope that declares the name, before overload resolution runs. The derived scale(int) hides every base scale, so a double argument is converted to int or the call fails. using Base::scale; restores them.

solid answer

~40 s

C++ resolves a call in two stages: **name lookup** first collects declarations of the name, then **overload resolution** picks the best of what lookup found. Lookup in a class searches the class itself and only moves on to base classes if the class declares nothing with that name. So once `Grid` declares `scale(int)`, every `Shape::scale` overload is **hidden**: `g.scale(1.5)` quietly calls `Grid::scale(int)` with 1, and `g.scale(1.0, 2.0)` does not compile. Signatures do not matter; the name alone hides. The fix is `using Shape::scale;` inside `Grid`, which brings the base overloads into the derived scope so all of them compete, or a qualified call such as `g.Shape::scale(1.0, 2.0)`. The same first-scope-wins rule applies to nested namespaces and blocks.

code

cpp · 26 lines
cpp
#include <iostream>

struct Shape {
    void scale(double f) { std::cout << "Shape::scale(double) " << f << '\n'; }
    void scale(double fx, double fy) { std::cout << "Shape::scale(double, double) " << fx << ' ' << fy << '\n'; }
};

struct Grid : Shape {
    void scale(int cells) { std::cout << "Grid::scale(int) " << cells << '\n'; }
};

struct FixedGrid : Shape {
    using Shape::scale;   // un-hide every Shape::scale overload
    void scale(int cells) { std::cout << "FixedGrid::scale(int) " << cells << '\n'; }
};

int main() {
    double factor = 1.5;
    Grid g;
    g.scale(factor);        // Grid::scale(int) 1  -- the double is truncated
    // g.scale(1.0, 2.0);   // error: Shape's two-argument scale is hidden
    FixedGrid f;
    f.scale(factor);        // Shape::scale(double) 1.5
    f.scale(1.0, 2.0);      // Shape::scale(double, double) 1 2
    f.scale(3);             // FixedGrid::scale(int) 3
}

go deeper

for a junior

Recall the effect: a member function in a derived class hides base-class members with the same name, and using Base::name; brings them back.

for a middle

Explain the two stages, lookup then overload resolution, and why the first scope that declares the name ends lookup no matter how poor its match is.

for a senior

Show the production cost: a silent narrowing call after someone added a derived overload, why no warning fired for a non-virtual function, and how a code review rule or using-declaration prevents it.

for a principal

Frame it as API design for class hierarchies: when a base exposes an overload set, derived classes either re-expose it with a using-declaration or use a different name, and the base author documents which.

## Lookup first, overload resolution second Every unqualified call in C++ goes through two separate steps, and confusing them is the root of this surprise: 1. **Name lookup** walks outward through scopes looking for declarations of the name. It stops at the first scope that declares the name at all. 2. **Overload resolution** then ranks only the candidates that lookup returned, by how well each parameter list matches the arguments. Overload resolution never goes back and asks an outer scope for more candidates. If the scope where lookup stopped holds a poor match, the poor match wins; if it holds no viable match, the call fails, even when a perfect match sits one scope further out. ## The rule in class scope For a member access such as `g.scale(...)`, lookup searches `Grid`'s own scope first. Only if `Grid` declares nothing named `scale` does it continue into the base classes. Declaring a single `scale` in `Grid` therefore **hides** every `scale` in `Shape`, whatever their parameter lists: ```cpp struct Shape { void scale(double f); void scale(double fx, double fy); }; struct Grid : Shape { void scale(int cells); // hides both Shape::scale overloads }; Grid g; double factor = 1.5; g.scale(factor); // Grid::scale(int), with 1 // g.scale(1.0, 2.0); // error: no viable scale in Grid ``` The first call is the dangerous one: it compiles and silently truncates. With a literal argument such as `g.scale(1.5)`, clang does warn under `-Wliteral-conversion`, but with a `double` variable, `-Wall -Wextra` stay quiet. Clang's error for the second call is `too many arguments to function call, expected 1, have 2; did you mean 'Shape::scale'?`. ## The same rule in namespaces and blocks Hiding is not a class feature; it is how lookup works in every scope: - A function `report(const char*)` in `geo::detail` hides `geo::report(double)` for code inside `geo::detail`, so `report(2.0)` there fails with no matching function, although the outer overload would match exactly. - A local variable named `distance` hides a namespace-scope function `distance` for the rest of the block. - A using-declaration in a block hides an outer name the same way an ordinary declaration does. Argument-dependent lookup can add candidates from the arguments' namespaces for unqualified function calls, but it does nothing for a `double` argument, which has no associated namespace, and it never applies to member calls like `g.scale(...)`. ## Ways to bring hidden names back | Fix | Where it goes | Effect | |---|---|---| | `using Shape::scale;` | inside `Grid` | all `Shape::scale` overloads join `Grid::scale(int)` in one overload set | | `g.Shape::scale(1.0, 2.0)` | at the call | qualified lookup in `Shape`; skips `Grid` entirely | | forwarding wrappers | inside `Grid` | re-declare each overload to call `Shape::scale`; verbose | | rename | either class | no hiding because the names differ | The using-declaration is the idiomatic fix. It also lets a derived class re-expose a base overload set while overriding just one member of it. ## Virtual functions and the warning that exists The same hiding happens when a derived class overrides only one overload of a virtual function. Here compilers help: - GCC's `-Woverloaded-virtual` warns when a declaration hides base-class virtual functions; its level 1 is part of `-Wall`, and the bare flag selects level 2, which also warns when the derived class overrides some overloads and hides others. - Clang has a warning of the same name, in its `-Wall` set. Both warnings cover **virtual** functions only. Hiding a non-virtual overload, as in `Grid` above, gets no diagnostic from either, which is why interviewers like the question. ## What to say in an interview - Lookup finds names, overload resolution picks among what lookup found, and the two never mix. - A derived class's declaration hides the base's whole overload set by name. - `using Base::name;` is the standard fix, and the reason the language lets a using-declaration appear in class scope at all. - Silent conversion to the derived overload is worse than a compile error, so check calls that pass a different type than the derived parameter expects.

  • Does making Shape::scale virtual stop Grid::scale(int) from hiding it?
    No. Hiding is decided by name lookup, and virtual changes dispatch, not lookup. A Grid::scale(int) still hides a virtual Shape::scale(double); it does not override it either, because the signatures differ. GCC's -Woverloaded-virtual and clang's warning of the same name flag exactly this case, and the fix is again using Shape::scale; or override with the matching signature.
  • Can a using-declaration in a derived class change the access of the base members it names?
    Yes. Base members named by a using-declaration in the derived class get the access of the point where the using-declaration appears. Placing using Shape::scale; under public: in a class that inherits Shape privately makes those overloads public members of the derived class, which is a common way to re-expose selected base functions.

  • Overload resolution looks at base-class overloads when the derived one is a poor match
  • Only an overload with exactly the same signature can hide a base function
  • Hiding only happens with virtual functions
  • g.scale(1.5) will call Shape::scale(double) because it is the exact match
  • using namespace Shape; inside the derived class brings the base overloads back