C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointeur de fonction membre

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Bruno CAUSSE
Guest





PostPosted: Mon Aug 22, 2005 9:36 am    Post subject: Pointeur de fonction membre Reply with quote



Comment écrire une fonction membre qui appelle un pointeur de fonction
membre

#define callMemberFunction(object,ptrToMember)  ((object).*(ptrToMember))

class Fred {

//Cela suffit (a l'interieur de la classe)
typedef  int (*FredMemberFn)(char, float);
//Ou faut il:
typedef  int (Fred::*FredMemberFn)(char, float);

//cela suffit (idem)
FredMemberFn a[4] = { &f, &g, &h, &i };
//Ou faut il
FredMemberFn a[4] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };


public:
int f(char x, float y);
int g(char x, float y);
int h(char x, float y);
int i(char x, float y);
      // ...

void userCode(int memberFunctionNum)

};

//est ce correcte?
void Fred::userCode(int memberFunctionNum)
{
// pré-supose que memberFunctionNum est compris entre 0 et 3 inclus:
callMemberFunction(this, a[memberFunctionNum]) ('x', 3.14);
}

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Aug 23, 2005 9:11 am    Post subject: Re: Pointeur de fonction membre Reply with quote



Bruno CAUSSE wrote:
Quote:
Comment écrire une fonction membre qui appelle un pointeur de
fonction membre

void
MaClasse::f( void (MaClasse::*pfm)() )
{
(this->*pfm)() ;
}

Quote:
#define callMemberFunction(object,ptrToMember) ((object).*(ptrToMember))

Ça pourrait marcher aussi :

callMemberFunction( *this, pfm )() ;

Franchement, je ne vois pas l'intérêt.

Quote:
class Fred {

//Cela suffit (a l'interieur de la classe)
typedef int (*FredMemberFn)(char, float);
//Ou faut il:
typedef int (Fred::*FredMemberFn)(char, float);

La seconde. La première déclare toujours un pointeur à une
fonction libre (ou à une fonction membre statique).

Quote:
//cela suffit (idem)
FredMemberFn a[4] = { &f, &g, &h, &i };
//Ou faut il
FredMemberFn a[4] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };

La séconde.

Quote:
public:
int f(char x, float y);
int g(char x, float y);
int h(char x, float y);
int i(char x, float y);
// ...

void userCode(int memberFunctionNum)

};

//est ce correcte?
void Fred::userCode(int memberFunctionNum)
{
// pré-supose que memberFunctionNum est compris entre 0 et 3 inclus:
callMemberFunction(this, a[memberFunctionNum]) ('x', 3.14);

(this->*(a[ memberFunctionNum ]))( 'x', 3.14 ) ;

Les parenthèses autour de l'accès au tableau ne sont pas
strictement nécessaire. L'utilisation de l'opérateur ->* est
assez peu fréquente, cependant, et je trouve donc qu'il y a une
risque que le programmeur moyen ne connaisse pas la précédance
tout de suite.

Avec ton macro, il faudrait *this, à la place de this.

Quote:
}

--
James Kanze GABI Software
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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.