1

I'm trying to assign a member function to a pointer to a member function in C++, but I am getting an error. 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_'?

4

2 回答 2

2

Jrok 是正确的,我解决了这个问题。我只是改变了:

MyClass* (MyClass::*memPtr_) (ParamType);

至...

MyClass& (MyClass::*memPtr_) (ParamType);

于 2013-09-09T19:10:00.813 回答
0

您不要使用 no 获取函数名的地址(),那就是指针。

于 2013-09-09T19:09:22.553 回答