Alex Rivera | Logout

C++ function pointer (class member) to non-static member function

Asked 2009-06-13T12:56:16.850
40
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.
}

How can I get this to work?

Edit
Report

1 Answer

-4

I think calling a non static member of the class could also be done using a static member function.

answered 2009-10-22T10:20:45.387

Your Answer