在C语言中,就是通过使用函数指针来实现C++中的多态的
#include<iostream>
using namespace std;
int max(int,int);
int min(int,int);
int add(int,int);
void process(int,int,int (*fun)(int,int));
void main()
{
    int a,b;
    cin>>a>>b;
    cout<<"max=";
    process(a,b,max);
    cout<<"min=";
    process(a,b,min);
    cout<<"add=";
    process(a,b,add);
}
int max(int x,int y)
{
    return ( x > y ? x : y ); 
}
int min(int x,int y)
{
    return ( x < y ? x : y ); 
}
int add(int x,int y)
{
    return ( x + y );
}
void process(int x,int y,int (*fun)(int,int))
{
    int result=fun(x,y);//result=(*fun)(x,y);
    cout<<fun<<" "<<*fun<<" ";
    cout<<result<<"\n";
}

运行结果:

C/C++函数指针用法

相关文章:

  • 2021-09-30
  • 2021-06-23
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
猜你喜欢
  • 2021-06-02
  • 2022-12-23
  • 2021-04-05
  • 2022-01-02
  • 2021-11-10
  • 2021-12-21
  • 2021-12-23
相关资源
相似解决方案