没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
The Function Pointer Tutorials
Introduction to C and C++ Function Pointers, Callbacks and Functors
written by Lars Haendel
January 2002, Dortmund, Germany
http://www.newty.de
email: lore@newty.de
version 2.041b
Copyright (c) 2000-2002 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
http://www.gnu.org .
Be aware that there may be a newer version of this document! Check
http://www.newty.de/zip/e 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
http://www.newty.de/zip/source.zip . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
1
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 c osts some time each call and maybe you can save some time using
function p ointers instead of virtual functions. Maybe not ...
1
1.1 What is a Function Pointer ?
Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind,
that a running program gets a certain space in the main-memory. Both, the exe cutable compiled program code
and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character
field, nothing else than an address. It is only important how you, or better your compiler/processor, interpret
the memory a pointer points to.
1.2 Introductory Example or How to Replace a Switch-Statement
When you want to call a function DoIt() at a certain point called label in your program, you just put the call
of the function DoIt() at the point label in your source code. Then you compile your code and every time your
program comes up to the point label, your function is called. Everything is ok. But what can you do, if you
don’t know at build-time which function has got to be called? What do you do, when you want to decide it
at runtime? Maybe you want to use a so called C allback-Function or you want to select one function out of a
pool of possible functions. However you can also solve the latter problem using a switch -s tateme nt, where you
call the functions just like you want it, in the different branches. But there’s still another way: Use a function
pointer! In the following example we regard the task to perform one of the four basic arithmetic operations.
The task is first solved using a switch-statement. Then it is shown, how the same can be done using a function
pointer.
2
//-----------------------------------------------------------------------------------------
// 1.2 Introductory Example or How to Replace a Switch-Statement
// Task: Perform one of the four basic arithmetic operations specified by the
// characters ’+’, ’-’, ’*’ or ’/’.
#include <iostream.h> // due to: cout
// the four arithmetic operations
// one of these functions is selected at runtime with a switch or a function pointer
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply (float a, float b) { return a*b; }
float Divide (float a, float b) { return a/b; }
// solution with a switch-statement - <opCode> specifies which operation to execute
void Switch(float a, float b, char opCode)
{
float result;
switch(opCode) // execute operation
{
case ’+’ : result = Plus (a, b); break;
case ’-’ : result = Minus (a, b); break;
1
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.
2
It’s only an example and the task is so easy that I suppose nobody wil l ever use a function pointer for it ;-)
2
case ’*’ : result = Multiply(a, b); break;
case ’/’ : result = Divide (a, b); break;
}
cout << "switch: 2+5=" << result << endl; // display result
}
// solution with a function pointer - <pt2Func> is a function pointer and points to
// a function which takes two floats and returns a float. The function pointer
// "specifies" which operation shall be executed.
void Switch_With_Function_Pointer(float a, float b, float (*pt2Func)(float, float))
{
float result = pt2Func(a, b); // call using function pointer
cout << "switch replaced by function pointer: 2-5="; // display result
cout << result << endl;
}
// execute example code
void Replace_A_Switch()
{
cout << endl << "Executing function ’Replace_A_Switch’" << endl;
Switch(2, 5, /* ’+’ specifies function ’Plus’ to be executed */ ’+’);
Switch_With_Function_Pointer(2, 5, /* pointer to function ’Minus’ */ &Minus);
}
Important note: A function pointer always points to a function with a specific signature! Thus all functions,
you want to use with the same function pointer, must have the same parameters and return-type!
2 The Syntax of C and C++ Function Pointers
Regarding their syntax, there are two different types of function pointers: On the one hand there are pointers
to ordinary C functions or static C++ member functions, on the other hand there are pointers to non-static
C++ member functions. The basic difference is that all pointers to non-static member functions need a hidden
argument: The this-pointer to an instance of the class. Always keep in mind: These two types of function
pointers are incompatible with each other.
2.1 Define a Function Pointer
Since a function pointer is nothing else than a variable, it must be defined as usual. In the following example
we define two function pointers named pt2Function and pt2Member. They point to functions, which take one
float and two char and return an int. In the C++ example it is assumed, that the function, our pointer points
to, is a member function of TMyClass.
// 2.1 define a function pointer
int (*pt2Function) (float, char, char); // C
int (TMyClass::*pt2Member)(float, char, char); // C++
2.2 Calling Convention
Normally you don’t have to think about a function’s calling convention: The compiler assumes cdecl as
default if you don’t specify another convention. However if you want to know more, keep on reading ... The
calling convention tells the compiler things like how to pass the arguments or how to generate the name of a
function. Examples for other calling conventions are stdcall, pascal, fastcall. The calling convention belongs
to a functions signature: Thus functions and function pointers with different calling convention
are incompatible with each other! For Borland and Microsoft compilers you specify a specific calling
convention between the return type and the function’s or function pointer’s name. For the GNU GCC you use
3
剩余12页未读,继续阅读
资源评论
phpjspasp
- 粉丝: 88
- 资源: 76
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功