如何创建指向成员函数的函数指针? [英] How create a function pointer to point a member function?
本文介绍了如何创建指向成员函数的函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
struct A
{
void* f(void *)
{
}
};
int main()
{
void * (*fp)(void *);
fp = &A::f;
}
编译:
|12|error: cannot convert 'void* (A::*)(void*)' to 'void* (*)(void*)' in assignment|
我尝试了很多方法,但是都失败了……该怎么做?
I have tried many ways, but all failed... How to do that?
推荐答案
对于常规成员指针,您需要声明其属于指针类型的成员的类型;
For a regular member pointer, you'll need to declare the type it's a member of on the pointer type;
int main()
{
void * (A::*fp)(void *);
fp = &A::f;
}
这篇关于如何创建指向成员函数的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文