Write a readable callback function in C++ language

In C++, callback functions can be implemented using function pointers, function objects, or Lambda expressions. Here is an example of a more readable callback function implemented using function pointers:

#include <iostream>  
#include <functional>  
  
// 定义一个回调函数类型  
using Callback = std::function<void(int)>;  
  
// 定义一个函数,用于演示回调函数的使用  
void performOperation(int data, Callback callback) {
    
      
    // 执行某些操作  
    std::cout << "Performing operation on data: " << data << std::endl;  
    // 调用回调函数  
    callback(data);  
}  
  
// 定义一个回调函数,用于接收操作结果并输出到控制台  
void handleResult(int result) {
    
      
    std::cout << "Result: " << result << std::endl;  
}  
  
int main() {
    
      
    // 调用performOperation函数,并传递一个回调函数作为参数  
    performOperation(100, handleResult);  
  
    return 0;  
}

In this example, we first define a Callbacktype, which is a function pointer type used to represent a function that can accept an integer parameter and return a null value. We then define a performOperationfunction that takes an integer argument and a callback function argument and performs some action. After performing the operation, we call the callback function and pass it the result of the operation as a parameter. Finally, we define a handleResultfunction as a callback function to receive the operation results and output them to the console. In the main function, we call performOperationthe function and handleResultpass it the function as a callback function.

It should be noted that the above example uses a function pointer as the callback function type. This method is more readable, but it requires manually defining and calling the callback function, so it may increase the complexity and maintainability of the code. If you need to improve the readability and maintainability of your code, you can consider using Lambda expressions or function objects as the implementation of callback functions.

operation result:

picture

Replenish:

A lambda expression is an anonymous function that can be passed as a parameter to other functions or used as an object. Lambda expressions are often used to simplify function definition and code simplicity.

In C++, the syntax of a Lambda expression is as follows:

capture -> return_type { function_body }
where:

  • capture: capture list, used to specify how external variables are introduced into Lambda expressions, which can be default values, namespaces or qualifiers.
  • parameters: parameter list, similar to function parameters, but the type and name can be omitted.
  • return_type: return type, can be omitted, the compiler will automatically deduce the type.
  • function_body: Function body, including the operation and logic of Lambda expression.

For example, here is an example of using a lambda expression to calculate the sum of two integers:

#include <iostream>  
#include <functional>  
  
int main() {
    
      
    int a = 10, b = 20;  
    auto add = [a, b](int x) -> int {
    
     return a + b + x; };  
    std::cout << add(5) << std::endl; // 输出35  
    return 0;  
}

In this example, we define a Lambda expression [a, b](int x) -> int { return a + b + x; }that accepts an integer parameter x and returns the sum of three integers. We assign this Lambda expression to a variable called add and call it in the main function passing an integer parameter 5. The final output is 35.

The syntax of Lambda expressions is relatively simple, but in actual use, you need to pay attention to the life cycle and scope of Lambda expressions, and how to correctly handle issues such as references and access permissions of external variables.

Guess you like

Origin blog.csdn.net/weixin_40933653/article/details/133318115