Discussion Series (4) - Function overload

zhaozj2021-02-11  165

The following article translated from Ian Joyner "C ?? a critique of C and program" 3 / e [Ian Joyner 1996] The original copyright belongs to Ian Joyner, and I am from the consent of Ian Joyner himself. This article is translated into Chinese. Therefore, the Chinese version of this article should belong to me ;-) The English and Chinese versions of this article are used for non-commercial purposes, you can copy and post it at will. However, it is best to add the previous declaration in front of the repost. If someone or agency wants to publish this article, please contact the original copyright owner and me.

In addition, this article has been included in the book "Objects Unencapsulated" written in Ian Joyner (there is already a translation version of Japanese), which is described in: http://www.prenhall.com/allbooks /PTR_0130142697.htmlhttp://efsa.sourceforge.net/cgi-bin/view/main/ObjectsuNCapsulatedHttp://www.accu.org/Bookreviews/public/reviews/O/O002284.htm

Ian Joyner Contact: I.Joyner@acm.org My Contact: cber@email.com.cn

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 the book herself "Objects Unencapsulated", fully demonstrates some of the deficiencies of C , we should fully learn from the great work he has completed, better understand C , thus Write more secure C code.

Discussion Series (4)

Function overload

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). Overloaded in the following examples is useful: max (int, int) max (real) This will ensure that the best MAX function entity relative to the 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. For a simple example of the above discussion, 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 an error in which type does not match. Of course, through the operation of the overload operator, such behavior can be better expressed as follows: I Max J or R Max S but MIN and MAX are special functions, they can accept two or more of the same Parameters of the type, and can also act on arbitrary lengths. Therefore, in EIFFEL, the most common code form of this situation seems like this: il: comparable_list [integer] rl: comparable_list [real] i: = il.max r: = rl.max above the example display, Object-oriented Programming Modem, especially when genericity is combined, the function of the function overload can be reached without the need to overload the function in C . 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).

转载请注明原文地址:https://www.9cbs.com/read-5396.html

New Post(0)