Issues in overloading and overriding classes

If you override an overloaded member function (virtual or not), your override hides all overloaded variants of that member function, not just the one you overrode. To properly override an overloaded member function, you must override all the overloaded variants. Of course, the overriding function can turn around and call the one from the base class.

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 {
          void Foo(long);
          void Foo(double);
      class B: public A {
          void Foo(double);               // Override hides Foo(long)
      B bar;
      bar.Foo(2);                         // Coerced to double
However, a call to an A object goes to A::Foo(long) because the override in B hides Foo(long) only in B's name scope, not in A's.

      A& br = bar;
      br.Foo(2);                          // Calls A::Foo(long)
NOTE The Taligent C++ Compiler warns you if you override some but not all of a set of overloaded virtual member functions.

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker