![]() |
|
#1
|
||||
|
||||
[Tutorial] Function PointersFunction pointers
The topic of function pointers may seem advanced to those who have never tried to use them, but they get easy very quickly! In this tutorial, I will teach the reader how to declare, assign, and call pointers to functions and pointers to functions that are members of a class. For those who do not know what pointers are or how to create them, go to this tutorial to learn how to do that. If you do know about pointers, please continue! The first thing we need to know about pointers to functions is that they are pointers. They are not actually functions, and they do not have a body. A function pointer is simply an address in the form of a function. Here is the syntax for creating a normal pointer compared to the syntax for creating a function pointer. CPP / C++ / C Code:
Ok, now that we have a function pointer, we need something to point to. This is where some strict rules come into play. There is one MAJOR rule about function pointers - the signature of the function pointer must be the same as the signature of the function that it points to. It is imperative that both functions have the same return type, number of parameters, type of parameters, and order of parameters. Now I will show you how to assign the address of a function to a function pointer. Although it's optional for most compilers you should use the address operator & infront of the function's name in order to write portable code. CPP / C++ / C Code:
CPP / C++ / C Code:
Here's another way to use function pointers. It's a simple concept, but I'll start off with a question. Have you ever had a switch statement that had a lot of branches, but all it did was decide what function to call based on the value of an integral variable (such as int or enum)? This can be simplified by creating an array of functions. Here's a quick example of what I mean. CPP / C++ / C Code:
CPP / C++ / C Code:
This concludes my tutorial on function pointers. If you have any questions or corrections, please feel free to post them on this forum. Questions will be answered to the best of my knowledge. Corrections will be implemented as I become aware of them. Please add some rep if you liked this tutorial! __________________
-Aaron Last edited by aaroncohn : 14-Apr-2005 at 02:17.
|
||||
|
#2
|
|||
|
|||
|
i have followed the code program like this:
CPP / C++ / C Code:
i have a c++ code to compile in borland c++ ,but the error shown up like this: funcs::fun1) is not accessible funcs::fun2() is not accessible funcs::fun3() is not accessible why does the message shown up ? how to reduce the error so the prog can run? Last edited by admin : 15-Feb-2006 at 23:05.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#3
|
||||
|
||||
Re: [Tutorial] Function PointersOf course... the default setting for members of a class is PRIVATE. You must declare them as PUBLIC in order to call them. I will correct this part of the tutorial
__________________
-Aaron |
|
#4
|
|||
|
|||
Re: [Tutorial] Function PointersQuote:
...or, you can employ the lazy-man's way and change "class" to "struct" and then everything is public by default! (I know, ugly--but works) Take Care. Rob! |
|
#5
|
|||
|
|||
Re: [Tutorial] Function PointersSomething that you may want to consider in your "next installment" of this topic is a demonstration of how to pass function pointers as arguments and to return function pointers. You may also want to show some common, real-world uses of functors...a term that I didn't notice being used in your text.
:davis: |
Recent GIDBlog
Non-US citizens serving in the military by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
| help with delete function using pointers!!! | wbsquared03 | CPP / C++ Forum | 0 | 29-Nov-2004 16:32 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The