Introduction to Function Pointers

Function Pointers provide some extremely interesting, efficient and elegant programming techniques. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks. Unfortunately – probably due to their complicated syntax – they are treated quite stepmotherly in most computer books and documentations. If at all, they are addressed quite briefly and superficially. They are less error prone than normal pointers cause you will never allocate or deallocate memory with them. All you’ve got to do is to understand what they are and to learn their syntax. But keep in mind: Always ask yourself if you really need a function pointer. It’s nice to realize one’s own late-binding but to use the existing structures of C++ may make your code more readable and clear. One aspect in the case of late-binding is runtime: If you call a virtual function, your program has got to determine which one has got to be called. It does this using a V-Table containing all the possible functions. This costs some time each call and maybe you can save some time using function pointers instead of virtual functions. Maybe not … BTW: Modern compilers are very good! With my Borland Compiler the time I was able to save calling a virtual function which multiplies two floats was about 2 percent. Baca lebih lanjut