Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90579
  • 博文数量: 50
  • 博客积分: 1086
  • 博客等级: 少尉
  • 技术积分: 420
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-25 16:16
文章分类
文章存档

2011年(50)

我的朋友

分类: C/C++

2011-11-15 19:05:38

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;

  4. void main()
  5. {
  6.  int max(int, int);
  7.  int (*p)(int, int) = &max;
  8.  int d = (*p)((*p)(19, 8), 7);
  9.  cout << d << endl;
  10. }
  11. int max(int a, int b)
  12. {
  13.  return a>b ? a:b;
  14. }


  15. 把函数名变成(*p)然后就成了 int (*p)(int, int) = &max;


  16. typedef int(*lpMax)(int, int); //宏定义函数指针类型


  17. #include "stdafx.h"
  18. #include <assert.h>
  19. #include <iostream>
  20. using namespace std;
  21. typedef int(* FuncP)(int, int);


  22. int add(int a, int b)
  23. {
  24. return a + b;
  25. }


  26. int multi(int a, int b)
  27. {
  28. return a*b;
  29. }


  30. int _tmain(int argc, _TCHAR* argv[])
  31. {
  32. FuncP fpDemo;
  33. fpDemo = (FuncP)add(1, 2);
  34. cout << (int)fpDemo << endl;
  35. fpDemo = (FuncP)multi;
  36. cout << fpDemo(2, 3) <<endl;
  37. // assert((fpDemo)(1, 2) == 3);
  38. return 0;
  39. }
  40. 暂时这么理解!!!
阅读(466) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~