Jump to content

Function overloading

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 103.225.132.9 (talk) at 05:12, 25 May 2017 (Rules in function overloading). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In some programming languages, function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.

For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second method, which would result in an ambiguous call error, as the compiler wouldn't know which of the two methods to use.

Another appropriate example would be a Print(object O) method. In this case one might like the method to be different when printing, for example, text or pictures. The two different methods may be overloaded as Print(text_object T); Print(image_object P). If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: Print(something).

Complications

Two issues interact with and complicate function overloading: name masking (due to scope) and implicit type conversion.

If a function is declared in one scope, and then another function with the same name is declared in an inner scope, there are two natural possible overloading behaviors: the inner declaration masks the outer declaration (regardless of signature), or both the inner declaration and the outer declaration are both included in the overload, with the inner declaration masking the outer declaration only if the signature matches. The first is taken in C++: "in C++, there is no overloading across scopes."[1] As a result, to obtain an overload set with functions declared in different scopes, one needs to explicitly import the functions from the outer scope into the inner scope, using the using keyword

Implicit type conversion complicates function overloading because if the types of arguments do not exactly match the signature of one of the overloaded functions, but can match after type conversion, resolution depends on which type conversion is chosen.

These can combine in confusing ways: an inexact match declared in an inner scope can mask an exact match declared in an outer scope, for instance.[1]

For example, to have a derived class with an overloaded function taking a double or an int, using the function taking an int from the base class, in C++, one would write

class B {
public:
    void f(const int i);
};

class D : public B {
public:
    using B::f;
    void f(const double d);
};

Failing to include the using results in an int argument passed to f in the derived class being converted to a double and matching the function in the derived class, rather than in the base class; including using results in an overload in the derived class and thus matching the function in the base class.

Caveats

If a method is designed with an excessive number of overloads, it may be difficult for developers to discern which overload is being called simply by reading the code. This is particularly true if some of the overloaded parameters are of types that are inherited types of other possible parameters (for example "object"). An IDE can perform the overload resolution and display (or navigate to) the correct overload.

Type based overloading can also hamper code maintenance, where code updates can accidentally change which method overload is chosen by the compiler.[2]

See also

References

  1. ^ a b "Why doesn't overloading work for derived classes?", Bjarne Stroustrup's C++ Style and Technique FAQ, Bjarne Stroustrup
  2. ^ Bracha, Gilad (September 3, 2009). "Systemic Overload". Room 101.
  • Bertrand Meyer: Overloading vs Object Technology, in Journal of Object-Oriented Programming (JOOP), vol. 14, no. 4, October–November 2001, available online