• 【DEMO】【C/C++】最简单的一种回调函数


    回调函数最简单用法

    // CallBackFunc.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    string toZeroDown(int n, void *contex) {
    	cout  << "toZeroDown:" << -*(int*)(contex) << endl;
    	return "toZeroDown";
    }
    
    string toZeroUp(int n, void *contex) {
    	cout << "toZeroUp:" << *(int*)(contex) << endl;
    	return "toZeroUp";
    }
    
    typedef string(*CallBackFunc)(int n, void *contex);
    
    void registNumCallBack(CallBackFunc callback, void *contex)
    {
    	int n = 3;
    	cout << "The Option of 'callback()' is:" << callback(n, contex) << endl;
    }
    
    int main()
    {
    	for (int i = 0; i < 10; i++)
    	{
    		if (i % 2) {
    			registNumCallBack(toZeroDown, &i);
    		}
    		else
    		{
    			registNumCallBack(toZeroUp, &i);
    		}
    	}
    	
    }
    

    输出:

    toZeroUp:0
    The Option of 'callback()' is:toZeroUp
    toZeroDown:-1
    The Option of 'callback()' is:toZeroDown
    toZeroUp:2
    The Option of 'callback()' is:toZeroUp
    toZeroDown:-3
    The Option of 'callback()' is:toZeroDown
    toZeroUp:4
    The Option of 'callback()' is:toZeroUp
    toZeroDown:-5
    The Option of 'callback()' is:toZeroDown
    toZeroUp:6
    The Option of 'callback()' is:toZeroUp
    toZeroDown:-7
    The Option of 'callback()' is:toZeroDown
    toZeroUp:8
    The Option of 'callback()' is:toZeroUp
    toZeroDown:-9
    The Option of 'callback()' is:toZeroDown
    

    原文:https://blog.csdn.net/Fuel_Ming/article/details/122950838

  • 相关阅读:
    c# 框架学习(nop )总结-------删除功能
    c# 框架学习(nop )总结-------编辑功能
    约束
    索引
    受限操作的变通解决方案
    删除数据表
    修改已有数据表
    定义外键
    定义主键
    定义默认值
  • 原文地址:https://www.cnblogs.com/xiaohai123/p/16315351.html
Copyright © 2020-2023  润新知