函数指针:指向函数的指针

刚学到这一点,记录一下

#include <bits/stdc++.h>
using namespace std;

// 指向函数的指针:函数指针
int compute(int a, int b, int(*func)(int, int))
{
  return func(a, b);
}

int max(int a, int b)
{
  return ((a > b) ? a : b);
}
int min(int a, int b)
{
  return ((a < b) ? a : b);
}
int sum(int a, int b)
{
  return a + b;
}

int main()
{
  // 此处写&或者不写都没有关系
  cout<<compute(12, 6, &max)<<" "<<compute(12, 6, min)<<" "<<compute(12, 6, &sum)<<endl;
  return 0;
}
发布了82 篇原创文章 · 获赞 71 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/m0_37738114/article/details/104794458