Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...