Mike wrote:
In C, we can typedef pointer to functions, and therefore use function
tables. But what's the advantage of using function table?
Thanks,
Mike
Example 1: Switch{ing} based on strings.
The C language does not allow a string variable type
inside a switch statement. One solution to this
dilemma is to use a table of <string, function_ptr>
records. Search for the string, and if found,
execute the function via the function pointer in
the record.
This is very handy in Menu processing for User
interfaces.
Example 2: Event delegation.
In many operating systems, they have a table
of functions to execute for a given event.
The table may have records such as
<event_ID, pointer to event function>
Again, match the event ID and execute the
function via the pointer.
Example 3: Factory pattern.
Example 4: Scripting.
One could have an engine that iteraters
through a table or list of function pointers,
executing each one. Each table or list can
be consider as a script or batch of instructions.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library