http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Identifiers in C++



right then.... depends what you want to be able to do this for.
if you want to have a shell within your program, to allow the user to type
in a command, and you want the command to translate directly to the
function, you'd be better off trying it with something like tcl or perl.
but if you need to do it in c, here's what you do:

construct a 2 dimensional array with strings and pointers to functions. e.g:

struct _cmds {
  char *command;
  int (*func)();
};

struct _cmds cmds[] = {
  { "cmd1", cmdfunc1 },
  { "cmd2", cmdfunc2 },
  { "cmd3", cmdfunc3 },
} ;

(don't forget that, like arrays, you can just use the function name to
obtain a pointer to the function)
then you need a couple of functions like the following:

c_comp(e1, e2)
struct _cmds *e1, *e2;
{
  return strncmp(e1->command, e2->command,4);
}

do_cmd(char *cmd)
{
  static int virgin = 1;
  struct _cmds key, *c = NULL;

  if (virgin) {
    virgin = 0;
    qsort(cmds, sizeof(cmds)/sizeof(cmds[0]), sizeof(cmds[0]), c_comp);
  }

  key.command = cmd;
  c = (struct _cmds *)
    bsearch(&key, cmds, sizeof(cmds)/sizeof(cmds[0]), sizeof(key), c_comp);
  if (c)
      c->func();
  else
      fprintf(stderr, "unknown command '%s'\n", cmd);
}

and there you have it - user calls do_cmd function with a string that
represents the function that is to be called (which could come from some
external source - can't think of any other reason you'd want to be doing
this anyway), and do_cmd calls the function. of course, it helps if all the
functions have very similar prototypes, or the do_cmd function will have to
be improved upon (and it'll probably be worth doing it an entirely different
way).
by the way, the above code was taken from the source for a daemon that we
have running here, and is heavily edited, so there could be errors. should
be enough to let you figure out the rest though. and i can't remember what
header files you need to include. *shrugs* anyway, there you go.

andy

--

Andy Burgess, Software Engineer.
Information Systems Production Software - 'PA' Listings.
eMail: andrewjb@padd.press.net
Phone: (01430) 488 224

----- Original Message -----
From: Karl Brehme <Karl@1planning.com.br>
To: <gameprogrammer@gameprogrammer.com>
Sent: Tuesday, September 14, 1999 1:36 PM
Subject: RE: Identifiers in C++


I think not.... not in normal ways... nor function or macro inside VC....

But you can try to construct your own... making indexes inside tables in
memory.... but.... is really complex, I think, considering that the eval
function of javascript, for example, that makes it, is a kind of function
that compiles on the fly the string... that can be watheer command line,
function, variable, from your code or from wathever function of all
APIs......

or.... change do Java... if JavaScript has it... Java maybe has it too...

Karl Heinz Brehme

-----Original Message-----
From: gameprogrammer-owner@gameprogrammer.com
[mailto:gameprogrammer-owner@gameprogrammer.com]On Behalf Of Ash
Stirling
Sent: Terça-feira, 14 de Setembro de 1999 07:58
To: gameprogrammer@gameprogrammer.com
Subject: Identifiers in C++


Hi,

  I'm trying to reference a variable by the contents of a variable.  That
is,

     x = "myVar";
y = myClass.(x);

  where the desired result is to have 7 = myClass.myVar .  Is this possible
under (MS) Visual C++ 4.0?  If I can do this, it will same me a lot of time
and will make my program much more flexible.  Thanks in advance.


Ash David Stirling                    Email: (ashley@roadrunner.nf.net)
Department of Computer Science Phone: (709) 739-0022
Memorial University of Newfoundland
St. John's, Newfoundland, Canada
=================================================================
To SUBSCRIBE or UNSUBSCRIBE please visit
http://gameprogrammer.com/mailinglist.html
======================
To SUBSCRIBE or UNSUBSCRIBE please visit
http://gameprogrammer.com/mailinglist.html

=================================================================
To SUBSCRIBE or UNSUBSCRIBE please visit
http://gameprogrammer.com/mailinglist.html