The callback callback function understands the equivalent of the this pointer

Source: Internet
Author: User

The 1.callback function defines the callback function in Microsoft's official manual: "The callback function is a function called by the application, defined by the operating system."
Any function that is designed by the user and called by the Windows system, collectively known as the callback function, has a certain type to match the call operation of Windows.
Some API functions use callback as one of their parameters, such as SetTimer, LineDDA, enumobjects. Typically, this API calls the callback function after a certain behavior or when a state is satisfied. Also, if the member function of a class is a callback function, it must be declared as "static" in order to remove the C + + compiler from a hidden parameter of the function.
Why do you want to remove this pointer? Because the callback function is called for Windows functions, Windows does not invoke the function with any object. If you remove the static, it causes a random variable in the stack to become the this pointer, and the result is a program crash. Remember: anything declared as static in a class is not combined with objects, they are part of the class and do not belong to the object.

2. For example, there is a hotel with wake-up service, but guests are asked to decide how to wake up. Can be a room phone, or send a waiter to knock on the door, sleep afraid of delay, you can also ask to pour water basin on their head. Here, the "Wake Up" behavior is provided by the hotel, which is equivalent to the library function, but the method of waking up is determined by the traveler and tells the hotel, that is, the callback function. And the traveler tells the hotel how to wake up their actions, that is, the callback function to transfer the action of the function, called Registering callback Functions(To register a callback function). As shown (image source: Wikipedia):

advantages of the callback mechanism

As you can see from the example above, the callback mechanism provides a lot of flexibility. Note that from now on, we are changing the library function in the diagram to call Intermediate Function, because callbacks are not just used between apps and libraries. At any time, you can take advantage of callbacks as long as you want flexibility similar to the situation above.
How is this flexibility implemented? At first glance, the callback appears to be just a call between functions, but a careful thought can reveal a key difference between the two: in the callback, we use a certain way to pass the callback function as a parameter into the intermediate function. It can be understood that the intermediate function is incomplete before passing in a callback function. In other words, the program can determine and change the behavior of the intermediate function at run time by registering different callback functions. This is much more flexible than a simple function call. Take a look at the following simple example of a callback written by Python:
<script>        function A (callback) {            alert ("The main function is done!") ");            Callback ();        }        Function B () {            alert ("Callback function b! ");        }        Function C () {            alert ("callback function C!) ");        }        The callback function is passed as a parameter, guaranteeing the flexibility of the variable        A (b);        A (c);</script>                        

Answer:

Using a function to make arguments has the following advantages: When you have a (b), function B is a callback function, and you can have a (c) at this time, the function C becomes the callback function. If you call it directly in function A, the callback function is limited to death. If you write function a () {...; b ();} The flexibility of the variable is lost.

The callback callback function understands the equivalent of the this pointer

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.