xsist10 wrote:
I'm going to assume you mean a pointer to a function, since the "method"
keyword is not something used in C++ very often.
whats the best method. delphi lets you do something very simple
I can't really think of a decent reason to want to do this anyway, more than
that I'm not sure its supported in C++, one thing that has been added to
C++ (not sure how long ago) is templates, these can be used to define
different values as part of functions and classes.
ptrMethodFoo : procedure
how do you do this in C++?
Regardless of this fact I did a search and came up with this following piece
of code, I've got to be honest and say that I didn't write this, it can be
found at the following address
krtkg1.rug.ac.be/~colle/C/function_pointers.html:
typedef struct {
char *name;
int (*function)(char *, char *);
} command_type;
int do_command1(char *, char *);
int do_command2(char *, char *);
command_type command_list[]={
{"command1", do_command1 },
{"command2", do_command2 },
{NULL, NULL } /*we end the list with a NULL*/
};
main()
{
while (1)
{
command_type *command;
char buf[BUFSIZ];
if (NULL == gets(buf))
break; /*end of input*/
/*check if the command is in the list*/
for(command=command_list; command->name != NULL; command++)
if (!strcmp(command->name, buf))
/*yes it is -- execute associated code*/
command->function("argument1", "argument2");
}
}