Overridden, overloaded functions are single entities | ||
C++ treats an overloaded function as a single entity because the C++ scope resolution rule is to find the first class containing any function that defines that | name, then to look for a match based on argument type. The C++ design team believes this is the correct rule; their reasoning is that an overloaded set of | functions is really just one function with several variants, and you should not name functions with the same name unless they are really the same function. |
In the following example, bar.Foo(2) calls B::Foo(double) after coercing the int
argument to double
. This is because the override of Foo(double) introduces the name Foo in B's scope, hiding that name from A's scope; again, the rule is that all overloaded variants constitute one name that is hidden or not.
class A { public: void Foo(long); void Foo(double); }; class B: public A { public: void Foo(double); // Override hides Foo(long) }; B bar; bar.Foo(2); // Coerced to double
A& br = bar; br.Foo(2); // Calls A::Foo(long)