Mail Archives: djgpp/1999/03/08/11:06:12
On Sun, 7 Mar 1999 18:18:30 +0000, Dave Bird <dave AT xemu DOT demon DOT co DOT uk>
wrote:
> You might want to write a clever sort algorithm which can sort many
> different kinds of data.
That's a good example. Here's another: Suppose you want to choose one
function out of a hundred to handle a specific chunk of data, like an
opcode handler for a emulator. Instead of having a switch statement
that's hundreds of lines long, as in:
switch(opcode)
{
case OPCODE1: OpcodeHandler1();
case OPCODE2: OpcodeHandler2();
...
case OPCODE254: OpcodeHandler254();
case OPCODE255: OpcodeHandler255();
};
You can have a clean, fast array of function pointers that is
initalized at the start. Then, when OPCODEX is read, the function at
the array index X is executed. Clean, fast, efficient. If you replace
the unused functions with a pointer to a default handler, it's even
better.
- Hicks
- Raw text -