当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - function pointer for a member function

问题描述:

it would be kind of someone to help with the issue:

i have a function within a class

class A

{

void fcn1(double *p, double *hx, int m, int n, void *adata);

void fcn2();

}

inside fcn2 i am trying to use pointer to fcn1 as follows:

A::fcn2()

{

void (*pfcn1)(double*, double*, int, int, void*) = fcn1;

}

and i am getting an error:

error C3867: 'A::fcn': function call missing argument list; use '&A::fcn' to create a pointer to member

it would be kind of someone to help.

Thanks

网友答案:

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?

You need to use a member function pointer instead:

void (A::*pfcn1)(double*, double*, int, int, void*) = &A::fcn1;

If you have an object a of type A, you can later call it using:

(a.*pfcn1)(&somedouble, &somedouble, 42, 69, NULL);

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);
网友答案:

Change to:

void (A::*pfcn1)(double*, double*, int, int, void*) = &A::fcn1;

Consider using a typedef for readability:

class A
{
   ...
   typedef void (A::*fcn1_ptr)(double*, double*, int, int, void*);
};

void A::fcn2()
{
    fcn1_ptr pfcn1 = &A::fcn1;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: