By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

What's the advantage of using function table

P: n/a
In C, we can typedef pointer to functions, and therefore use function
tables. But what's the advantage of using function table?

Thanks,

Mike
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.