|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
what is the difference in : typedef void (fn) (); and typedef void (*fn) (); the first one is used by my course reader for passing callback functions as arguments to other functions. the second one is used in a command dispatch table which is basically a map with the strings as keys(names of functions) and function pointers as values. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 2007-12-10 05:22:25 -0500, "riddhi.mittal@gmail.com"
<riddhi.mittal@gmail.com> said: > > what is the difference in : > > typedef void (fn) (); > > and > > typedef void (*fn) (); > > the first one is used by my course reader for passing callback > functions as arguments to other functions. No, it's not. Those other functions don't take this type, they take pointers to this type. > the second one is used in a command dispatch table which is basically > a map with the strings as keys(names of functions) and function > pointers as values. Yup. The first is a function type and the second is a pointer to that same function type. It's exactly the same as the difference between int and int* Well, almost; a function type can't be used in as many places as an int. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Dec 10, 2:09 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2007-12-10 05:22:25 -0500, "riddhi.mit...@gmail.com" > <riddhi.mit...@gmail.com> said: > > what is the difference in : > > typedef void (fn) (); > > and > > typedef void (*fn) (); > > the first one is used by my course reader for passing callback > > functions as arguments to other functions. > No, it's not. Those other functions don't take this type, they take > pointers to this type. > > the second one is used in a command dispatch table which is basically > > a map with the strings as keys(names of functions) and function > > pointers as values. > Yup. The first is a function type and the second is a pointer to that > same function type. It's exactly the same as the difference between int > and int* Well, almost; a function type can't be used in as many places > as an int. What's probably confusing him is the fact that when used to declare a parameter to a function, a function type is remapped to a pointer to function type, much in the same way an array is remapped to a pointer. So if he has something like: void registerCallback( fn ) ; , this is implicitly converted to: void registerCallback( fn* ) ; if the first typedef is used. It's one of those obfuscation features C++ inherited from C. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|