function pointer

# function pointer - The Function Pointer Tutorials...

• Homework Help
• 25

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

2.1 Define a Function Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 Calling Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.3 Assign an Address to a Function Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.4 Comparing Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.5 Calling a Function using a Function Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.6 How to Pass a Function Pointer as an Argument ? . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.7 How to Return a Function Pointer ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.8 How to Use Arrays of Function Pointers ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3 How to Implement Callback Functions in C and C++ 7 3.1 Introduction to the Concept of Callback Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2 How to Implement a Callback in C ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.3 Example Code of the Usage of qsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.4 How to Implement a Callback to a static C++ Member Function ? . . . . . . . . . . . . . . . . . 8 3.5 How to Implement a Callback to a non-static C++ Member Function ? . . . . . . . . . . . . . . 9 4 Functors to encapsulate C and C++ Function Pointers 11 4.1 What are Functors ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4.2 How to Implement Functors ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4.3 Example of How to Use Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 11 Introduction to Function Pointers Function Pointers provide some extremely interesting, eﬃcient 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 brieﬂy and superficially. They are less error prone
than normal pointers cause you will never allocate or de-allocate 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

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Fall '11
• Santelices
• Data Structures, Function object, Function pointer, TClassA, TClassB

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern