PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Function pointers
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Function pointers

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2007, 10h22   #1
riddhi.mittal@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Function pointers


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.
  Réponse avec citation
Vieux 10/12/2007, 13h09   #2
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function pointers

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)

  Réponse avec citation
Vieux 11/12/2007, 09h05   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function pointers

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11726 seconds with 11 queries