Overloaded

from Wikipedia, the free encyclopedia

Overloading (of English overloading in software development) means the conscious establishment of polymorphisms , so that one and the same language element - operators , constructors , literals , and the like - different information to emerge from the context meanings can accept. Since it is a purely syntactical mechanism, overloading is regarded as an ad hoc polymorphism according to Strachey .

Not all programming languages allow overloading; as a method, it is found particularly in object-oriented programming languages.

Explanation using an example

In many programming languages ​​the operator stands +for different, quite different, operations:

1 + 2
"abc" + "def"

In the first case the expected result is "3", that is an integer addition , in the second it is "abcdef", that is a string concatenation . These are basically two very different operations. Compilers or interpreters of languages ​​that allow such a so-called polymorphism do this by selecting the method to be used (addition or concatenation) based on the data type of the operands involved.

Of overcharging is called now, if such a polymorphism not only, as in the case of the example, prefabricated exists in one language, but in more of such polymorphisms can be created also by language constructs. In the example above, for example, the language could offer the possibility of establishing an additional meaning of the +operator for other data types (such as arrays) through program text. (This would be an example of operator overloading .)

Method overload

Method overload occurs when several methods have the same name, but they expect different parameters ( arguments ). The exact method that is called is then automatically determined by the compiler each time it is called based on the parameters and their data types. A typical example is a method that should be able to output both text ( strings ) and whole numbers ( integers ) on the screen. Example from Pascal :

procedure GibAus(text: String); overload;
begin
    writeln(text); // übergebenen Parameter ausgeben
end;

procedure GibAus(zahl: Integer); overload;
var
    zahlAlsText: String;
begin
    zahlAlsText := IntToStr(zahl); // übergebene Zahl in einen Text umwandeln
    writeln(zahlAlsText); // ausgeben
end;

begin
    GibAus('Hallo Welt!'); // gibt den Text "Hallo Welt!" aus
    GibAus(4711);          // gibt die Zahl 4711 aus
end;

In the case of functions , overloading based on the result type is also possible in some languages .

The disadvantage is a loss of clarity and that problems can arise if further overloads are subsequently added (see the Wikibook linked below).

Demarcation

Default values

No method overloading in the true sense is the default value (Engl. Default argument ), which is also to lead that - but the same - with different method, namely different numbers, parameters can be called. If a parameter is omitted, the default value is transferred instead:

procedure GibAus(text: string = 'kein Parameter übergeben');
begin
    writeln(text); // übergebenen Parameter ausgeben
end;

begin
    GibAus('Hallo Welt!'); // gibt "Hallo Welt!" aus
    GibAus();              // gibt den Standardwert "kein Parameter übergeben" aus
end;

Methods with a default value can easily be converted into overloaded methods. The above source code is functionally identical to the following:

procedure GibAus(text: String); overload;
begin
    writeln(text); // übergebenen Parameter ausgeben
end;

procedure GibAus(); overload;
begin
    GibAus('kein Parameter übergeben'); // fehlenden Parameter ergänzen
end;

begin
    GibAus('Hallo Welt!'); // gibt "Hallo Welt!" aus
    GibAus();              // gibt den Standardwert "kein Parameter übergeben" aus
end;

This is useful in languages ​​that do not support default values, such as Java.

Implicit casts

Another distinction to be made between method overloads is implicit type conversions . In many programming languages, a single method that expects a floating point value (real) as a value parameter can also be called with a whole number (integer). An implicit type conversion is not possible for a reference parameter , since formal and actual parameters must match:

function QuadriereWertparameter(basis: Real): Real;
begin
    result := basis * basis; // Ergebnis als Funktionswert zurückgeben
end;

procedure QuadriereReferenzparameter(var basis: Real);
begin
    basis := basis * basis; // Ergebnis dorthin schreiben, wo der Parameter herkam
end;

var
    zahl: Integer;
begin
    zahl := 4711;
    QuadriereWertparameter(zahl); // implizite Typumwandlung (Ergebnis der Funktion wird verworfen)
    QuadriereReferenzparameter(zahl); // Compilerfehler, denn die aufgerufene Methode könnte keine Kommazahl zurückgeben, obwohl eine Kommazahl als Referenzparameter deklariert wurde
end;

If you were to add the following overloaded method (and the overloaddirective for the existing method) to the source code above , the program would be compilable:

procedure QuadriereReferenzparameter(var basis: Integer); overload;
begin
    basis := basis * basis;
end;

Operator overload

Has always distinguished the most programming, the mathematical Following tradition, not between the operator symbols ( +, -, ...) for integer ( Integer -) and floating point (real, float). For seamless extension of a language with user-defined types, it is helpful to be able to overload the operator symbols for user-defined types.

An example of operator overloading +in C ++ :

struct komplexe_zahl {
    float real = 0;
    float imag = 0;
};

komplexe_zahl operator+(komplexe_zahl links, komplexe_zahl rechts) {
    return komplexe_zahl {links.real + rechts.real, links.imag + rechts.imag};
}

int main() {
    komplexe_zahl z1 {5.0f, 3.14159f};
    komplexe_zahl z2 {0.0f, -3.14159f};
    komplexe_zahl z3 = z1 + z2; // z3.m_real = 5 und z3.m_imag = 0
}

In C ++, almost all existing operators can be overloaded, which in most cases can be done both via free-standing functions (as here) and via methods.

The aim of operator overloading is the easy readability of the source code, which should always be considered to what extent this is improved by overloaded operators or worse: For example, while (a + b) * cclearly better to read than mal(plus(a, b), c)this is by no means always the case, especially if the concrete overcharging not well follows known conventions.

Finally, it should be noted that operator overloading is only a matter of syntactic sugar and, as a rule, it would be just as possible to implement the desired functionality using normal functions or methods

See also

Web links

  • Overload , Wikibook Structured Programming