Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1460459
  • 博文数量: 254
  • 博客积分: 8696
  • 博客等级: 中将
  • 技术积分: 2961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 16:46
文章分类

全部博文(254)

文章存档

2015年(4)

2014年(18)

2013年(16)

2012年(8)

2011年(25)

2010年(2)

2009年(74)

2008年(107)

分类: C/C++

2014-03-27 18:02:58

The type of pointer-to-member-function is different from pointer-to-function.
The type of a function is different depending on whether it is an ordinary function or a non-static member function of some class:
int f(int x);
the type is "int (*)(int)" // since it is an ordinary function

And

int Mat::f2(int x);
the type is "int (Mat::*)(int)" // since it is a non-static member function of class Mat

Note: if it's a static member function of class Fred, its type is the same as if it were an ordinary function: "int (*)(char,float)"

    In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs pointer-to-function) are different and incompatible. C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object.

    NOTE: do not attempt to "cast" a pointer-to-member-function into a pointer-to-function; the result is undefined and probably disastrous. E.g., a pointer-to-member-function is not required to contain the machine address of the appropriate function. As was said in the last example, if you have a pointer to a regular C function, use either a top-level (non-member) function, or a static (class) member function.

src web:

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