A deep comparative analysis of the design principle of function call and callback function

Source: Internet
Author: User

Why do I think about dissecting this topic? This is when I work with a partner in the harmonious function of docking with the callback function, let me harvest quite abundant. For a long time, but this question in yesterday's Read COM programming book, Inside again used the callback function mechanism. Before the summary, and then a busy, forget. Dongguan Development Piao Now this, I also found the charm of the callback function, so, quickly summed up an article, and enjoy with the king.
at first we learn the callback function, which always acknowledges the callback function in the system, calls our function, and then arrives so that we can manipulate part of the action. However, the need for homework is applied to this mechanism. Because the partner does the underlying function, it interacts with my function module. In order to make the base database more general, but also allow the upper layer to freely customize the action, after all, choose the callback function mechanism. At first, I just felt that it should be done with a function call, and later understood, only to find the inadequacy of the function call. Let me introduce the mechanism of function call and callback function, Harbin Development Piao lets you find the charm of the callback function. There is no longer a sense of horror in the callback function, but an infinite excitement.
let's illustrate with two functions.

/*-The first method is the function call. -*/void A () {... B (); ....}


The ellipsis represents the execution of some operations in the meantime, and then calls the B function, and finally performs some operations, the a function finishes. This is a brief analysis. Let's make a further analysis.
when an external function calls a function, it is the code that fulfills the a function. Fulfilling the meaning of the code, Kunming Piao is that the CPU will fulfill the machine code generated by a function. From another point of view, that is, a function at the moment to manipulate the CPU's fulfillment process, the development of Anyang Piao that a function has the control right. After performing a piece of code, the B function is called. Right now, the control has shifted to B. Finally, after the B function comes back, the control has come back to a. Finally, the rest of the code is executed.
Let's take a look at the second way, the callback function.



Pfunb is a self-defining function type pointer, defined as follows:
typedef void (*PFUNB) ();
Pfunb Pfunb;
After this definition, PFUNB is the function pointer type of b function, using PFUNB to define a function variable Pfun, pfun to fulfill the function call.
In fact, this kind of toss-up is a callback function. Maybe you don't have a thorough understanding of what's going on right now. Here is a specific explanation.
Pfun is a function pointer, the natural ability to assign values to the function pointer, Nanjing Piao assigned value is a function name, that is, the address of the function fulfillment body. After this is assigned, the function pointer call function and the General B () call is no different, but it is a callback function mechanism. This is the same way as it is in the parsing of the calling function. and its special place is described below, this is the charm of the callback function location. Without this charm, Microsoft will not use so many callback mechanisms to provide us with a mechanism for custom action.
The purpose of the callback function mechanism planning is to provide a sensitive action customization ability when the underlying code is not altered. How to say it? In general, in the planning of a base library, to consider that the base library is frequently used, neijiang generation development Piao and demand is very strong. It is also important that the base library cannot be changed often, or that a doubt can cause fatal damage to the software. Changsha Development Piao For example, a common gap in Microsoft's system, such as the core layer of the bedrock of the code there is a gap, it will cause all goods into crisis. These effects are appropriate and harsh. Therefore, the base library can not easily change, once implemented, after a rigorous test, the future will not be easy to amend.
However, the base library to let the function of the upper layer to complete the sensitive customization, assuming the use of function calls, it is certainly unrealistic. Because the bottom of the library to know the upper function of the information, to let the upper function to provide good code completion, not perhaps. As the underlying library is developed, it may not be possible to know what is going on at the top, let alone to provide the upper level of code completion. If it is not supplied, then the function call cannot be fulfilled. Code completion is when you call a function and know how the code in the function performs. That is, there is the completion of the function. Shanghai Development Piao as the base, as the foundation, is certainly the earliest performance, at the moment the upper function do not know where, at the moment how to know the code of the upper layer? Suppose you say that the code can be stored first to the base library, then this is called the base library? Thus the function code you stored is the bedrock of the base. This is the function call, which requires the called function to exist beforehand, otherwise it cannot be called.
        But the thought is slightly changed, and the thing is Vista. Already unable to supply the upper function code to complete, then can supply a function of the pointer bar. Look, this is also the ferocious place of the pointer.
        Why is the supply pointer possible? C + + provides the initial operation support for the pointer. At the moment, the code that does not need to supply the upper function is completed. This function can only be called by this pointer when the underlying library is running. If the function is not completed after the pointer calls this upper function, perhaps the pointer value is wrong, then it will cause the collapsibility. In the case of accuracy, the function pointer is now assigned the correct value before the underlying library runs the A function. When a is fulfilled, it is able to invoke the function performance accurately. This is done on the basis of the skill.
        on the other hand, suppose that the function that the base library is calling is a function of the same class, which is able to supply the function to complete, and thus can be called smoothly. However, the question came. Assuming that there are many functions in the future, you need to revise the base to provide support action customization may function custom, Kunming hair Piao At this moment you are not very distressed. Because it is often a bad effect to revise the base library. So the customization and extensibility has brought great inconvenience.
        while in the base library, the call is provided as a function pointer, you can customize the action of the function at any time. Just according to the Convention, before performing this function of the base library, assigning values to function pointers accurately, using your custom functions to assign value, you can use your function's fulfillment action in the Base library function, then arrive at the action custom, and the extensibility is very good. In the expansion of functions and custom actions, the foundation will not have to revise the base library of this function. Then the development of Piao in Anyang,  can also ensure that the base library will not introduce fault in the frequent amendments.
        After the above analysis, I think you should have a good understanding of the callback function mechanism at the moment. If you find a doubt, please put forward, if you do not understand, also remember to raise OH.

A deep comparative analysis of the design principles of the function call and the callback function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.