简体   繁体   English

成员函数的函数指针

[英]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: 在fcn2里面我试图使用指向fcn1的指针,如下所示:

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; 错误C3867:'A :: fcn':函数调用缺少参数列表; use '&A::fcn' to create a pointer to member 使用'&A :: fcn'创建指向成员的指针

it would be kind of someone to help. 这将是一种帮助的人。

Thanks 谢谢

Change to: 改成:

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

Consider using a typedef for readability: 考虑使用typedef来提高可读性:

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

void A::fcn2()
{
    fcn1_ptr pfcn1 = &A::fcn1;
}

fcn1() is not a plain function but a member function. fcn1()不是普通函数,而是成员函数。 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? 不能使用普通函数指针来保存它的指针,因为这并不能提供足够的信息:我应该this设置,当函数被调用来?

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类型的对象a ,稍后可以使用以下方法调用它:

(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: 如果你有一个指向类型A的对象的指针pa ,你可以稍后使用它来调用它:

(pa->*pfcn1)(&somedouble, &somedouble, 42, 69, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM