Path: utzoo!attcan!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Pointers to member-functions, what's wrong? Message-ID: <476@taumet.com> Date: 15 Oct 90 17:01:14 GMT References: Distribution: comp Organization: Taumetric Corporation, San Diego Lines: 42 ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: >In short, how do I make this work? What's wrong? >#include >class c >{ >public: > void f1() { cout << "Function 1...\n"; } > void f2() { cout << "Function 2...\n"; } > void dispatch( void (*func)() ) { (*this.*func)(); } >}; >main() >{ > c x; > x.dispatch(c::f1); > x.dispatch(c::f2); >} The problem is that a pointer to a member function is a different kind of thing than a pointer to an ordinary function. There are two errors in your example, and it is the errors that Turbo C++ doesn't like; I'm sure it likes you just fine. Here is the corrected example: #include class c { public: void f1() { cout << "Function 1...\n"; } void f2() { cout << "Function 2...\n"; } void dispatch( void (c::*func)() ) { (this->*f)(); } ^^^^^^^^^^ ^^^^^^^^^^^^ }; main() { c x; x.dispatch(c::f1); x.dispatch(c::f2); } -- Steve Clamage, TauMetric Corp, steve@taumet.com