FunctionPointers - The Function Pointer Tutorials...

Info icon This preview shows pages 1–3. Sign up to view the full content.

The Function Pointer Tutorials Introduction to C and C++ Function Pointers, Callbacks and Functors written by Lars Haendel September 2001, Dortmund, Germany email: [email protected] version 2.02b Copyright (c) 2000/2001 by Lars Haendel. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front- Cover Text being the text from the title up to the table of contents, and with no Back-Cover Texts. A copy of the license can be obtained from . Be aware that there may be a newer version of this document! Check fpt.pdf for the latest release. If you want to distribute this document, I suggest you to link to the URL above to prevent spreading of outdated versions. You may also download the source code of the examples at . The example code is free software under the terms of the GNU General Public License. Contents 1 Introduction to Function Pointers 2 1.1 What is a Function Pointer ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Introductory Example or How to Replace a Switch -Statement . . . . . . . . . . . . . . . . . . . . 2 2 The Syntax of C and C++ Function Pointers 3 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 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 ? . . . . . . . . . . . . . . 8 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 1
Image of page 1

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

1 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 . Unfor- tunately – 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 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 using
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '16
  • KP
  • Data Structures, Function object, Function pointer, Functors, TClassA

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern