Date: Tue, 25 Jul 2000 10:06:45 +0200 (WET) From: Andris Pavenis To: Damian Yerrick cc: djgpp AT delorie DOT com Subject: Re: Pointer to a function in class? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Tue, 25 Jul 2000, Damian Yerrick wrote: > On Mon, 24 Jul 2000 19:26:04 GMT, sami3079 AT my-deja DOT com wrote: > > >How do I get a pointer to a function in class? > > To be able to get pointers to class methods in C++, generally you have > to mark the function pointed to as "static" in the class declaration, > which means it doesn't get passed a pointer to `this'. > > Seriously, pointers to methods aren't useful unless you're trying to > interface your C++ code with a library written in C (e.g. Allegro or > something). If you're trying to interface C++ with C++, inheritance > and polymorphism (the `virtual' keyword) provide a cleaner mechanism. > Try following example: #include class foo { public: int funct1 (void) { cout << "foo::Funct1\n"; } int funct2 (void) { cout << "foo::Funct2\n"; } }; int main (void) { foo f1; int (foo::*funct[])(void) = { &foo::funct1, &foo::funct2 }; for (int i=0; i<2; i++) (f1.*funct[i])(); for (int i=0; i<2; i++) ((&f1)->*funct[i])(); return 0; } And read some good book about C++... Andris PS. This is tests example only. If You don't have real application for such features, You most likely don't really need it Also this stuff is off topic in DJGPP mailing list. More appropriate place would be some C++ related newsgroup