什么是回调
函数指针有什么用呢?一个最常用的地方就是回调。
什么回调?维基百科是这样解释的:回调是一段可执行的代码通过参数传递给别一段代码,以期望在一个合适的时间调用这个参数(可执行的代码)。
从一个需求开始
假设有这么一个需求,有一Person类定义如下:
struct Person
{
int age;
float weight;
float height;
};
现要对Person的一组对象进行排序,但并没有确定根据什么规则来排序,有时需要根据年龄进行排序,有时需要根据身高进行排序,有时可能是根据身高和体重的综合情况来排序,还有可能……
你可能会想到这样写,定义三个函数分别根据年龄、体重、身高进行排序:
void SortByAge(Person* persons, int count);
void SortByWeight(Person* persons, int count);
void SortByHeight(Person* persons, int count);
如果要根据身高和体重的综合情况来排序,那你还得再定义一个函数。这样是不是代码冗余且很繁琐?但如果你会用回调,这个问题就会很简单。
用回调实现对Person的排序:
- typedef int (*Compare)(const Person&, const Person&);
- //交换两个元素
- void swap(Person* p1, Person *p2)
- {
- Person p = *p1;
- *p1 = *p2;
- *p2 = p;
- }
- //排序(本例中采用冒泡排序)
- void PersonSort(Person* persons, int count, Compare pCompare)
- {
- for (int i = 0; i < count-1; i ++)
- {
- for (int j = 0; j < count - i -1; j++)
- {
- if (pCompare(persons[j], persons[j+1]) > 0)
- {
- swap(persons+j, persons+j+1);
- }
- }
- }
- }
如果你要根据年龄来进行排序,只要实现一个Compare类型的函数,再调用上面的PersonSort函数就可以实现根据年龄排序的功能。如:
- int CompareByAge(const Person& p1, const Person& p2)
- {
- return p1.age - p2.age;
- }
- void TestCallBack()
- {
- //创建Person的一组对象persons,对象中的各个值为0到100的随机数
- srand((unsigned)time(NULL));
- Person persons[10];
- for(int i = 0; i < 10; i ++)
- {
- persons[i].age = rand()%100;
- persons[i].weight = rand()%100;
- persons[i].height = rand()%100;
- }
- //【todo】
- //根据年龄进行排序。
- PersonSort(persons, 10, CompareByAge);
- for(int i = 0; i < 10; i ++)
- {
- std::cout << persons[i].age << "\t" << persons[i].weight << "\t" << persons[i].height << std::endl;
- }
- }
说明:以上调用TestCallBack函数时需要包含头文件<iostream>、<time.h>
这样如果需求发生变更(如要根据每个Person身高和体重的总和来排序),只需求再定义一个Compare类型的函数,而不用再对PersonSort函数做任何改动。如下:
- int CompareByHeightWeight(const Person& p1, const Person& p2)
- {
- return (p1.height + p1.weight) - (p2.height + p2.weight);
- }
- void TestCallBack()
- {
- //创建Person的一组对象persons,对象中的各个值为0到100的随机数
- srand((unsigned)time(NULL));
- Person persons[10];
- for(int i = 0; i < 10; i ++)
- {
- persons[i].age = rand()%100;
- persons[i].weight = rand()%100;
- persons[i].height = rand()%100;
- }
- //【todo】
- //根据年龄进行排序。
- PersonSort(persons, 10, CompareByHeightWeight);
- for(int i = 0; i < 10; i ++)
- {
- std::cout << persons[i].age << "\t" << persons[i].weight << "\t" << persons[i].height << "\t" << persons[i].weight + persons[i].height << std::endl;
- }
- }
C++ STL中的Sort(在<algorithm>头文件中)用的就是这种技术:
template<class RandomAccessIterator> void sort( RandomAccessIterator first, RandomAccessIterator last ); template<class RandomAccessIterator, class Predicate> void sort( RandomAccessIterator first, RandomAccessIterator last, Predicate comp );
回调函数说白了就是定义一个函数,然后通过参数传递给另一个函数调用。回调不仅是一种技术,更是一种编程思想,上面是通过回调函数来实现的,但它不仅限于回调函数,也可以用其它的技术实现(如面向对象的实现)。