What's the advantage of using function table

M

Mike

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

Thanks,

Mike
 
T

Thomas Matthews

Mike said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top