Callback Function usage (communication between classes) and callback function usage Communication

Source: Internet
Author: User

Callback Function usage (communication between classes) and callback function usage Communication

// Leleapplication3.cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> # include <functional> using namespace std; // 1" steering wheel "class receives external operations and transmits messages to the" car "class, the vehicle class transmits messages to the "Wheel" class. // (subclasses send messages to the parent class) // 2 "steering wheel" class receives external operations, pass the message to the "Wheel" class // (send a message to the subclass) // The class Steering {private: function <void (float)> m_steeringAction; public: // set the callback function void setWheelConnectWithCar (function <void (float)> steeringAction) {m_steeringAction = steeringAction;} // turn the steering wheel void turn (float angle) {cout <"Steering turn" <angle <"angle" <endl; m_steeringAction (angle) ;}; // Wheel class Wheel {public: // turn the Wheel to void turn (float angle) {cout <"Wheel turn" <angle <"angle" <endl ;}}; // Car class Car {public: // although the Steering Wheel is in the Car, you can directly operate Steering m_steering; Wheel m_wheel; Car () {setCarConnectWithWheel ();} # if 1 // 1 "steering wheel" receives external operations and transmits messages to the "car" class, the vehicle class transmits the message to the "Wheel" class. // void setCarConnectWithWheel () {std: function <void (float)> _ fun = std :: bind (& Car: steeringAction, this, std: placeholders: _ 1); m_steering.setWheelConnectWithCar (_ fun);} // this function is called when the steering wheel is turned, then change the function so that the wheel turns the corresponding angle void steeringAction (float angle) {m_wheel.turn (angle) ;}# else // 2 "steering wheel" class to receive external operations, pass the message to the "Wheel" class // void setCarConnectWithWheel () {std: function <void (float)> _ fun = std :: bind (& Wheel: turn, & m_wheel, std: placeholders: _ 1); m_steering.setWheelConnectWithCar (_ fun) ;}# endif}; int _ tmain (int argc, _ TCHAR * argv []) {Car _ car; // rotate the steering wheel by 30 degrees _ car. m_steering.turn (30); return 0 ;}


How can a member function in a class be used as a callback function? I want to access the instance of this class through the callback function.

As a callback, the member function of the class must pass the class member pointer along with the pointer pointing to this member function;

If callback to a class member function is not supported, declare this function as a class static function.

Usage and benefits of callback Functions

Function call forms are not good or bad.

Any function code error may cause software problems or even crash.

Pointers and function pointers can be used in c.
This makes the program more flexible.

When a function pointer is passed as a function parameter to a called function,
The called function can call an external function through this pointer, which forms a callback.

In windows, WndProc is a typical form of callback.

In a general program, callback functions are not very effective. You can skip this form.

The main purpose is to call a function when it is not in the same file, such as a dynamic library.
Functions in other programs only adopt callback.

# Include "stdio. h"
# Include "conio. h"

Int add (int a, int B );
Int libfun (int (* pDis) (int a, int B ));

Int main (void)
{
Int (* pfun) (int a, int B );

Pfun = add;
Libfun (pfun );

}

Int add (int a, int B)
{
Return a + B;

}

Int libfun (int (* pDis) (int a, int B ))
{
Int a, B;
A = 1;
B = 2;
Printf ("% d", pDis (a, B ));

}

These functions are in the same file.

Assume that
Int libfun (int (* pDis) (int a, int B ))
Is a function in the library, only the callback is used, the external function address is passed through the function pointer Parameter
To call

The Code of function add is modified, and you can call the function normally without modifying the library code.
Easy program maintenance and upgrade

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.