Pointeur de fonction C++ (membre de la classe) à la non-fonction membre statique

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.
}

Comment puis-je obtenir que cela fonctionne?

  • Ne sont pas func_x et func_y fonctions statiques, même si elles ne sont pas marqués comme tels?
InformationsquelleAutor Girish | 2009-06-13