相关疑难解决方法(0)

C++函数指针(类成员)到非静态成员函数

class Foo {
public:
    Foo() { do_something = &Foo::func_x; }

    int (Foo::*do_something)(int);   // function pointer to class member function

    void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; }

private:
    int func_x(int m) { return m *= 5; }
    int func_y(int n) { return n *= 6; }
};

int
main()
{
    Foo f;
    f.setFunc(false);
    return (f.*do_something)(5);  // <- Not ok. Compile error.
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它发挥作用?

c++ function-pointers

39
推荐指数
2
解决办法
6万
查看次数

标签 统计

c++ ×1

function-pointers ×1