函数回调测试
test_callback.h
#pragma once
#include <iostream>
void printWelcome(int len);
void printGoodbye(int len);
void callback(int times, void(*print)(int));
test_callback.cpp
#include "test_callback.h"
using namespace std;
void printWelcome(int len)
{
cout << "welcome " << len << endl;
}
void printGoodbye(int len)
{
cout << "goodbye " << len << endl;
}
void callback(int times, void(*print)(int))
{
int i;
for (i = 0; i < times; ++i)
{
print(i);
}
cout << "welcome or goodbye?" << endl;
}
main_test_callback.cpp
#include "test_callback.h"
#include <iostream>
using namespace std;
void main(void)
{
callback(10, printWelcome);
callback(10, printGoodbye);
printWelcome(5);
system("pause");
}
运行结果: