[英]How to assign a member function to a pointer to member function in C++
I'm trying to assign a member function to a pointer to a member function in C++, but I am getting an error. 我正在尝试将成员函数分配给C ++中的成员函数指针,但出现错误。 I have code like this:
我有这样的代码:
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass {
MyClass* (MyClass::*memPtr_) (ParamType);
public:
void myFunction();
void myFunction2();
void myFunction3();
MyClass& foo(ParamType var);
MyClass& bar(ParamType var);
MyClass& fooBar(ParamType var);
...
};
#endif
and... 和...
void MyClass::myFunction() {
memPtr_ = &MyClass::foo;
... ...
void MyClass::myFunction2() {
memPtr_ = &MyClass::bar;
... ...
void MyClass::myFunction3() {
memPtr_ = &MyClass::fooBar;
... ...
And on the lines 并在线
memPtr_ = &MyClass::foo;
and 和
memPtr_ = &MyClass::bar;
and 和
memPtr_ = &MyClass::fooBar;
when I compile - I get the error: 当我编译时-我得到了错误:
"cannot convert 'MyClass& (MyClass::*)(ParamType)'
to MyClass* (MyClass::*)(ParamType)' in assignment."
I've searched - but I can't seem to find the solution to this problem - what is the correct syntax to assign a member function to the pointer to member function 'memPtr_'? 我进行了搜索-但似乎无法找到解决该问题的方法-将成员函数分配给指向成员函数'memPtr_'的指针的正确语法是什么?
Jrok is correct, and I fixed the problem. Jrok是正确的,我已解决了该问题。 I simply changed:
我只是改变了:
MyClass* (MyClass::*memPtr_) (ParamType); MyClass *(MyClass :: * memPtr_)(ParamType);
to... 至...
MyClass& (MyClass::*memPtr_) (ParamType); MyClass&(MyClass :: * memPtr_)(ParamType);
您不要使用没有()
的函数名的地址,这就是指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.