17 November 2018

Function Pointer Object example in C++

 using namespace std;  
 typedef std::function<double(int)> funcIntToDouble;  
 void Test(funcIntToDouble f, int x)  
 {  
      double b = f(x);  
      cout << b;  
 }  
 int main()  
 {  
      cout << "function object example" << endl;  
      int a = 10;  
      funcIntToDouble f = [&](int x) {  
           double r = (double)(x + a) / 10; //able use use variable a from the calling function  
           return r;  
      };  
      Test(f, 15); //pass the function pointer along with value from variables in scope  
      getchar();  
   return 0;  
 }  

No comments:

Post a Comment