Mail Archives: djgpp/2000/07/25/05:03:27
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 <iostream>
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
- Raw text -