2008-10-31 7 views

Répondre

2

Oui, vous pouvez.

+0

D'accord +1. Ouf, j'aimerais qu'ils soient aussi faciles. – TheSoftwareJedi

+0

int (* a) (intq) { return q + 1; } cette définition fonctionne-t-elle? –

+0

@ramu: non, cela ne fonctionne pas. –

13

Oui. Exemple trivial:


// Functions that will be executed via pointer. 
int add(int i, int j) { return i+j; } 
int subtract(int i, int j) {return i-j; } 

// Enum selects one of the functions 
typedef enum { 
    ADD, 
    SUBTRACT 
} OP; 

// Calculate the sum or difference of two ints. 
int math(int i, int j, OP op) 
{ 
    int (*func)(int i, int j); // Function pointer. 

    // Set the function pointer based on the specified operation. 
    switch (op) 
    { 
    case ADD:  func = add;  break; 
    case SUBTRACT: func = subtract; break; 
    default: 
     // Handle error 
    } 

    return (*func)(i, j); // Call the selected function. 
} 

+0

Greg: merci pour l'édition en surbrillance - apprenez quelque chose de nouveau tous les jours! –

1

Oui. Un exemple:

Avant le code ...

 
typedef int (_stdcall *FilterTypeTranslatorType) 
    (
     int TypeOfImportRecord, 
     PMAType *PMA 
    ); 


FilterTypeTranslatorType FilterTypeTranslator = {NULL}; 

maintenant dans le code ...

 
PMAType *PMA; 
HANDLE hFilterDll; 

// assume DLL loaded 
// Now find the address... 
... 
     FilterTypeTranslator[TheGroup] = 
      (FilterTypeTranslatorType) GetProcAddress(hFilterDll, 
                 "FilterTypeTranslator"); 
... 
// now call it 


FilterTypeTranslator(1,PMA); 
...