一个MFC C++ 回调非静态成员函数的问题,请大家看看问题出在了哪里。 non-standard syntax; use '&' to create a pointer to member
问题描述:
两个类:
class A
{
public:
// Construction
A();
virtual ~A();
// Attributes
public:
void (CWnd::*m_fpDragCol)(int&, int&);
void SetCallback(void (CWnd::*fpDragCol)(int&, int&));
......
};
SetCallback的实现如下:
void A::SetCallback(void (CWnd::*fpDragCol)(int&, int&))
{
m_fpDragCol = fpDragCol;
}
class B
{
...
void functon1();
void function2(int &source, int &dest);
}
functon1与functon2的实现如下:
void B::fucntion1()
{
A a = new A;
a->SetCallback((void (CWnd::*)(int&, int&))function2);
}
void B::function2(int &source, int &dest)
{
//do something
}
现在编译报错:
C3867 'B::function2': non-standard syntax; use '&' to create a pointer to member
请问大家问题出在了哪里
答
是不是
a->SetCallback((void (CWnd:: * )(int&, int&))function2);
要改为
a->SetCallback((void (CWnd:: * )(int&, int&))&function2);
再说你这个回调函数要求指定的回调函数是 CWnd类的函数啊
答
这个是成员方法指针的语法,用&B::funtion2这种二形式