Returns an example pointer to a function in C++

  • 2020-04-02 01:31:11
  • OfStack

In C++, the parameter of a function can be a pointer to a function or a function can return a pointer to a function.
Such as:
Int (* ff (int)) (int x, int); Ff (int) is a function with an int parameter. The function returns an int (*) (int *,int).
Using a typedef makes the definition more understandable:
Typedeff int (*PF) (int *,int);
PF ff (int);
Here's an example:


#include<iostream>
using namespace std;
void fff(int *i,int j)
{
  cout<<"fff"<<endl;
}
void (*f(int))(int* pi,int i)
{
  cout<<"cheng"<<endl;
  return fff;
}
typedef void (*((*pf)(int)))(int*,int);
int main()
{
  pf p;
  p=f;
  int i;
  int *ip;
  p(i);
  return 0;
}

Output results:
Cheng,


Related articles: