成员函数的函数指针 [英] function pointer for a member function
问题描述
这将是一个帮助这个问题的人:
it would be kind of someone to help with the issue:
我有一个类内的函数
class A
{
void fcn1(double *p, double *hx, int m, int n, void *adata);
void fcn2();
}
里面fcn2我试图使用指针fcn1如下:
inside fcn2 i am trying to use pointer to fcn1 as follows:
A::fcn2()
{
void (*pfcn1)(double*, double*, int, int, void*) = fcn1;
}
我得到一个错误:
错误C3867:'A :: fcn':函数调用缺少参数列表;使用& A :: fcn'创建指向成员的指针
error C3867: 'A::fcn': function call missing argument list; use '&A::fcn' to create a pointer to member
这将是一种帮助。
感谢
推荐答案
fcn1
不是一个简单的函数,而是一个成员函数。你不能使用普通的函数指针来存储指向它的指针,因为这不能提供足够的信息:当调用函数时,应该如何设置 this
?
fcn1()
is not a plain function but a member function. You can't use an ordinary function pointer to store a pointer to it, because this doesn't provide enough information: what should this
be set to when the function is called?
您需要使用成员函数指针:
void (A::*pfcn1)(double*, double*, int, int, void*) = &A::fcn1;
如果你有一个对象 a
A
,您可以稍后使用
If you have an object a
of type A
, you can later call it using:
(a.*pfcn1)(&somedouble, &somedouble, 42, 69, NULL);
如果您有一个指针 pa
A 类型的对象,稍后可以使用
If you have a pointer pa
to an object of type A
, you can later call it using:
(pa->*pfcn1)(&somedouble, &somedouble, 42, 69, NULL);
这篇关于成员函数的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!