c++函数指针

 

1.声明函数指针

  声明指向某种数据类型的指针时,必须指定指针指向的类型。同样,声明指向函数的指针时,也必须指定指针指向的函数类型。这意味着声明应指定函数的返回类型一级函数的特征标(参数列表)。也就是说,声明应像函数原型那样指出有关函数的信息。
  例如:

    

double pam(int); //prototype

  则正确的指针类型声明如下:

double (*pf) (int);//pf points to a function that takes one int argument and that return type double

  这与pam()声明类似,这是将pam替换为(*pf)。由于pam是函数,因此(*pf)也是函数。而如果(*pf)是函数,则pf就是函数指针。

  为了提供正确的运算符优先级,必须在声明中是用括号将*pf括起。括号的优先级比*运算符高,因此*pf(int)意味着pf()是一个返回指针的函数,而(*pf)(int)意味着pf是一个指向函数的指针。

2.使用指针来调用函数

pf = pam;                         //pf指向函数pam()
double x = pam(4);           //用函数名调用函数pam()
double y = (*pf)(4);          //用函数指针pf调用函数pam()

3.函数指针示例1

 

 1 #include <iostream>
 2 
 3 double betsy(int);
 4 double pam(int);
 5 
 6 void estimate(int lines, double (*pt) (int));
 7 
 8 
 9 int main()
10 {
11     using namespace std;
12     int code;
13     cout << "How many lines of code do you need? ";
14     cin >> code;
15     cout<<"Here's Betsy's estimate\n";
16     estimate(code,betsy);
17     cout << "Here's Pam's estimate:\n";
18     estimate(code, pam);
19     cin.get();
20     cin.get();
21     return 0;
22 }
23 
24 double betsy(int lns)
25 {
26     return 0.05 * lns;
27 }
28 
29 double pam(int lns)
30 {
31     return 0.03 * lns + 0.0004 * lns * lns;
32 }
33 
34 void estimate(int lines, double(*pf)(int))
35 {
36     using namespace std;
37     cout << lines<<" lines will take ";
38     cout << (*pf)(lines) << " hour(s)\n";
39 }

执行结果

函数指针实例2

 1 #include <iostream>
 2 double add(double x, double y)
 3 {
 4     return x + y;
 5 }
 6 
 7 double sub(double x, double y)
 8 {
 9     return x-y;
10 }
11 
12 double div(double x, double y)
13 {
14     return x/y;
15 }
16 double calculate(double,double,double(*add)(double,double));
17 
18 int main()
19 {
20     using namespace std;
21     double x, y; 
22     double(*pf[3])(double,double) = {add, sub, div};
23     while(cin>>x>>y)
24     {
25         cout<<"sum = "<<(*pf[0])(x,y)<<endl;
26         cout<<"sub = "<<(*pf[1])(x,y)<<endl;
27         cout<<"div = "<<(*pf[2])(x,y)<<endl;
28         cout<<"sum = "<<calculate(x,y,pf[0])<<endl;
29         cout<<"sub = "<<calculate(x,y,pf[1])<<endl;
30         cout<<"div = "<<calculate(x,y,pf[2])<<endl;
31     }
32 
33     cin.get();
34     cin.get();
35     return 0;
36     
37 }
38 
39 double calculate(double x,double y,double(*padd)(double,double))
40 {
41     return (*padd)(x,y);
42 }

 

深入函数指针示例

 1 #include <iostream>
 2  
 3  
 4  const double * f1(const double ar[], int n);
 5  const double * f2(const double [], int);
 6  const double * f3(const double *, int);
 7  
 8  int main()
 9  {
10      using namespace std;
11      double av[3] = {1112.3, 1542.6, 2227.9};
12  
13      const double * (*p1)(const double *,int) = f1;
14      auto p2 = f2;
15      cout << "Using pointers to functions:\n";
16      cout << "Address Value\n";
17      cout << (*p1)(av,3) << ": " << *(*p1)(av,3) <<endl;
18      cout << p2(av,3) << ": " << *p2(av,3)<<endl;
19  
20      const double *(*pa[3])(const double *, int) = {f1, f2, f3};
21      
22      auto pb = pa;
23      cout << "\nUsing an array of pointers to functions:\n";
24      cout << " Address Value\n";
25      for (int i = 0; i < 3; i++)
26      {
27          cout<<pa[i](av, 3) << ": " << *pa[i](av, 3) <<endl;
28      }
29      cout << "Using a pointer to a pointer to a function:\n";
30      cout << " Address Value\n";
31      for (int i = 0; i < 3; i++)
32      {
33          cout<<pb[i](av, 3) << ": " << *pb[i](av, 3) <<endl;
34      }
35  
36      cout << "Using a pointer to an array of function:\n";
37      cout << " Address Value\n";
38  
39      auto pc = &pa;
40  
41      cout << (*pc)[0](av, 3) << ": "<< *(*pc)[0](av, 3) <<endl;
42      const double *(*(*pd)[3])(const double *, int) = &pa;
43      const double *pdb = (*pd)[1](av,3);
44      cout<<pdb << ": "<< *pdb << endl;
45      cout<< (*(*pd)[2])(av,3) << ": " << *(*(*pd)[2])(av,3)<<endl;
46      cin.get();
47      cin.get();
48      return 0;
49  }
50  
51  const double * f1(const double ar[], int n)
52  {
53      return ar;
54  }
55  const double * f2(const double ar[], int)
56  {
57      return ar + 1;
58  }
59  const double * f3(const double * ar, int)
60  {
61      return ar + 2;
62  }

 

执行结果

 

 

posted @ 2012-09-04 20:44  Asan825  阅读(202)  评论(0编辑  收藏  举报