回调函数的例子

    技术2026-01-18  4

    //para_callback.h

    #pragma once

     

    typedef void (*callback_t)(void *);

    extern void repeat_three_times(callback_t, void *);

     

     

    //para_callback.cpp

    #include "StdAfx.h"

    #include "para_callback.h"

     

    void repeat_three_times(callback_t f, void *para)

    {

    f(para);

    f(para);

    f(para);

    }

     

     

    //Main.cpp

    #include "stdafx.h"

    #include "para_callback.h"

    #include <Windows.h>

     

    void say_hello(void *str)

    {

    printf("Hello %s/n", (const char *)str);

    }

     

    void count_numbers(void *num)

    {

    int i;

    for(i=1; i<=(int)num; i++)

    printf("%d ", i);

    putchar('/n');

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

    repeat_three_times(say_hello, "Guys");

    repeat_three_times(count_numbers, (void *)4);

     

    system("pause");

    return 0;

    }

     

    最新回复(0)