C Critical Series: Function Overload
Ian Joyner
Cber translation
Translator's preface: If you want to completely master a language, you need to know what it is, but also know what it is in the shortcomings. This way we can use this language to avoid some traps in the language to better use this language to serve our work. This article of Ian Joyner and his Objects Inncapsulated book, fully showed some of C 's shortcomings, we should fully borrow the great work he has completed, better understand C , thus writing More secure C code.
C allows the overload function to be overloaded in a different type of parameter type. The overloaded function is different from a function of polymorphism (ie, virtual functions) in: calling the correct overloaded function entity is determined during the compilation period; and for a polymorphism, it is Call the function entity we want to call by the dynamic binding during operation. Polymorphism is achieved by redefining (or rewriting). Please don't be confused by overloading and overriding. The overload is in the case where two or more functions have the same name. The way to distinguish them is to be implemented by detecting their parameters or types. The overload is different from multiple distributions in CLOS, and the multiple distribution of parameters is done during operation.
[READE 89] points out the difference between overload and polymorphism. Overload means using the same name in the same context to replace different function entities (which has a completely different definition and parameter type). Polymorphism has only one definition body, and all types are subjected to a subtype with the most basic type. C. Strachey pointed out that polymorphism is a parameterized polymorphism, and the overload is a special polymorphism. The mechanism for judging different overload functions is the function indication (Function Signature).
The overload is useful in the example below:
Max (int, int)
Max (Real, REAL)
This will ensure that the best MAX function entity relative to Type INT and REAL is called. However, object-oriented programming provides a variable for this function, the object itself is passed as a hidden parameter to the function (in C , we call it this). Because of this, in the object-oriented concept, it is implicitly included, but more more limited. A simple example of the above discussion is as follows:
INT I, J;
Real r, s;
I.max (j);
R.max (s);
But if we write: i.max (r), or R.max (j), the compiler will tell us that there is a type of mismatched in this. Of course, through the operation of the overload operator, such behavior can be better expressed as follows:
i MAX J or
R Max S
However, MIN and MAX are special functions that can accept two or more of the same type of parameters, and can also act on arbitrary lengths. Therefore, in Eiffel, it looks like this for this most common code form:
Il: comparable_list [integer]
RL: Comparable_list [Real]
I: = il.max
R: = rl.max
The above example shows that the object-oriented programming model, especially when genericity is combined, and the function of the function overload can be reached without the need to overload the function in C overload. However, C makes this concept more generally. The benefit of this C is that we can reach the purpose of overloading by more than one parameter, rather than using only a hidden current object as a parameter. Another factor we need to consider is, which overload function decided is called to be completed during the compilation phase, but for rewriting, it will go to the run. This seems that the overload can make us more performance benefits. However, during the global analysis, the compiler can detect whether the functions of the function min and the max are inherited, and then they can call them directly (if yes). That is, the compiler checks the objects I and R, and then analyzes the MAX function corresponding to their, and found that there is no polymorphism in this case, so that the above statement has been directly called MAX. Target code. In contrast, if the object n is defined as a number, Number provides an abstract MAX function declaration (all the real.max and interger.max we use from it), then the compiler will be This generates dynamically binding code. This is because n may be Integer, or may be Real.
Now that you don't think this method of C (ie, by providing different parameters to implement the overloader)? However, you must also understand that object-oriented programming has a variety of restrictions, there is a lot of rules. C is to implement it in a manner that must be in line with the base class by specifying parameters. The parameters in the incoming function can only be a base class, or a derived class of a base class. E.g:
A.f (b Someb) {...}
Class B ...;
Class D: Public B ...;
A a a;
D D;
A.f (d);
Where D must be consistent with class 'b', the compiler will detect this.
Another possible way to implement function overload by different function signs is to make different functions in different names to make their signatures. We should use the name as a foundation that distinguishes different entities. The compiler can cross to detect the meticulum that we provide in line with the specified functions. This also leads to a better self-recording software (Self-document). From the similar name, choose a given entity will not be easy, but it is really worth doing this.
[Wiener95] provides an example to show the problem of overloading virtual functions:
Class Parent
{
PUBLIC:
Virutal Int Doit (INT V)
{
Return V * V;
}
}
Class Child: Public Parent: Public Parent: PUBLIC PARENT
{
PUBLIC:
INT DOIT (int V, int av = 20)
{
Return V * av;
}
}
int main ()
{
INT I;
Parent * p = new child ();
I = P-> DOIT (3);
Return 0;
}
What will I equal after the program is executed? Some people may think that it is 60, but the result is 9. This is because DOIT's signature in Child is inconsistent in the Parent. It does not rewrite the DOIT in the Parent, but only overloads it, in which case the default value does not work. Java also provides method overload, different methods can have the same name and different signatures.
No new technologies are introduced in Eiffel, but use quarterization, inheritance, and redefine. Eiffel provides a cohabeled signature, which means that there is no need to fully comply with the signature in the subclass of the parent class, but strong type detection techniques via Eiffel make them match each other.