From oleg Tue Aug 9 10:43:35 CDT 1994 Newsgroups: comp.lang.c++ Subject: Pointers to class methods: taking, storing, using [Hack] Summary: Non-prescribed but more traditional use of method pointers Followup-To: Distribution: Organization: University of North Texas, Denton Keywords: class method pointers, function pointers, call dispatching Cc: Status: RO It appears that questions about taking a pointer of a class method pop up on and off. There is a "standard" way of doing that, using notation like void (ClassFoo::*fooptr)(void). However, doing things this way involves a bit odd syntax. I stumbled on a more traditional way of taking pointers of class methods, storing them in a table for call dispatching, and using them. Here's the snippet (which is a maimed version of a real *working* program). I apologize that even the trimmed down version turns out bigger than what seems appropriate. I just wanted to show a bit more of a context. And also that it works correctly both for methods with no arguments and for methods with arguments. class PGPShell : public XVTDialog { PGPOptions options; BOOLEAN handle_ctl(const short id, const CONTROL_INFO& ctl); public: PGPShell(const short resource_id); ~PGPShell(void) {} protected: // Executors typedef BOOLEAN (*Executor)(PGPShell * _this); BOOLEAN do_save_config(void); BOOLEAN do_reload_config(void); BOOLEAN do_help(void); //... etc.... // Specialized Editors of some // dialog fields typedef BOOLEAN (*Editor)(PGPShell * _this, const short item_id, const int misc_arg); void change_option_bool(const short item_id, const int key); void change_option_str(const short item_id, const int key); }; // Handle some control events BOOLEAN PGPShell::handle_ctl(const short id, const CONTROL_INFO& ctl) { // Check first for action buttons static struct ExecutorDesc { short item_id; Executor executor; } Executors [] = { { IDP_HELP, (Executor)&do_help}, // here the pointer is taken { IDP_SAVOPT, (Executor)&do_save_config}, { IDP_REVOPT, (Executor)&do_reload_config} }; register int i; for(i=0; i