【发布时间】:2010-11-02 16:49:55
【问题描述】:
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.
}
我怎样才能让它工作?
【问题讨论】:
-
func_x和func_y不是静态函数,即使它们没有被标记为静态函数?
标签: c++ function-pointers