Is the type of “pointer-to-member-function” different from “pointer-to-function”?
2015-12-17 04:16
495 查看
Is the type of “pointer-to-member-function” different from “pointer-to-function”?
Yep.Consider the following function:
int f(char a, float b);
The type of this function is different depending on whether it is an ordinary function or a non-
staticmemberfunction of some class:
Its type is “
int (*)(char,float)” if an ordinary function
Its type is “
int (Fred::*)(char,float)” if a non-
staticmember function of
class
Fred
Note: if it’s a
staticmember function of
class
Fred, its type is the same as if it were an ordinary function:“
int (*)(char,float)”.
example code:
#include <iostream>
#include <string>
using std::string;
class Foo{
public:
int f(string str){
std::cout<<"Foo::f()"<<std::endl;
return 1;
}
};
int main(int argc, char* argv[]){
int (Foo::*fptr) (string) = &Foo::f;
Foo obj;
(obj.*fptr)("str");//call: Foo::f() through an object
Foo* p=&obj;
(p->*fptr)("str");//call: Foo::f() through a pointer
}
#include <iostream> #include <string> using std::string; class Foo{ public: static int f(string str){ std::cout<<"Foo::f()"<<std::endl; return 1; } }; int main(int argc, char* argv[]){ //int (Foo::*fptr) (string) = &Foo::f;//error int (*fptr) (string) = &Foo::f;//correct (*fptr)("str");//call Foo::f() }
https://isocpp.org/wiki/faq/pointers-to-members
http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm
相关文章推荐
- sgu194 Reactor Cooling【无源汇有上下界可行流】
- [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
- 【jQuery】:selected选中状态选择器
- 【jQuery】:checked选中状态选择器
- 【jQuery】:button表单按钮选择器
- 【jQuery】:image图像域选择器
- 【jQuery】:submit提交按钮选择器
- 【jQuery】:checkbox复选框选择器
- 【jQuery】:radio单选按钮选择器
- 【jQuery】:password表单密码选择器
- 【jQuery】:text表单文本选择器
- 【jQuery】:input表单选择器
- nwjs 开发桌面应用
- JavaScript复习——02....大致的了解
- Lo-Dash 与 underscore,Prototype 与 jQuery,两段恩怨情仇
- 【jQuery】:last-child子元素过滤选择器
- html 获取宽高
- 使用Caffe尝试DeepID
- javascript数组的知识点讲解
- NodeJS用递归实现异步操作的链式调用,完成一个简易的命令行输入输出REPL交互接口