Chinaunix首页 | 论坛 | 博客
  • 博客访问: 712268
  • 博文数量: 60
  • 博客积分: 2849
  • 博客等级: 少校
  • 技术积分: 1011
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-10 15:38
文章分类
文章存档

2013年(4)

2012年(11)

2011年(5)

2010年(3)

2009年(9)

2008年(19)

2007年(9)

分类: C/C++

2008-05-24 21:54:48

  1 #include
  2 #include
  3 #include
  4 #include
  5
  6 typedef void (*func_type)(void * obj, int num);
  7 class test_t
  8 {
  9 public:
 10         void test_func(int num)
 11         {
 12                 printf("num is %d\n", num);
 13         }
 14
 15         int a;
 16         int b;
 17
 18 };
 19
 20
 21 int main(int argc, char* argv[])
 22 {
 23         test_t obj;
 24         func_type p = &test_t::test_func;
 25         for(int i = 100; i < 104; i++)
 26                 p(&obj, i);
 27
 28         return 0;
 29 }
 30

程序比较简单,注意它的编译参数:
g++  -Wno-pmf-conversions   -O hello.cpp && ./a.out
否则编译不能通过。

阅读(1846) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-11 13:39:59

6.7 Extracting the function pointer from a bound pointer to member function In C++, pointer to member functions (PMFs) are implemented using a wide pointer of sorts to handle all the possible call mechanisms; the PMF needs to store information about how to adjust the `this' pointer, and if the function pointed to is virtual, where to find the vtable, and where in the vtable to look for the member function. If you are using PMFs in an inner loop, you should really reconsider that decision. If