2012-12-06 3 views
2

Je voudrais donner une instruction en tant que paramètre:Exécuter une instruction plus tard

execute_at_frame(int frame_number, <instruction>) 
{ 
    for(f = 1 ; f < F_MAX ; f++) 
    { 
     /* other instructions */ 
     if (f == frame_number) 
      /* execute <instruction> */ 
     /* other instructions */ 
    } 
} 
  • Un type d'appel: execute_at_frame(5,execute(42));
  • Un autre type d'appel: execute_at_frame(6,process());

Est-ce (ou quelque chose de similaire) possible?

Merci d'avance :-)

Répondre

3

Oui, si vous utilisez std::bind (11 C++):

template <class F> 
void execute_at_frame(int frame_number, F instruction) 
{ 
    for(int f = 1 ; f < F_MAX ; f++) 
    { 
     /* other instructions */ 
     if (f == frame_number) 
      instruction(); 
     /* other instructions */ 
    } 
} 

/* ... */ 

execute_at_frame(5,process); // no bind for functions without parameters 
execute_at_frame(5,std::bind(execute,42)); 

Sinon, vous devrez préparer une interface pour les instructions.

2

Votre paramètre <instruction> peut être soit un pointeur de fonction (par exemple un pointeur sur une fonction execute); ou, il peut être une référence à une instance d'une classe, qui a une méthode execute.

1

Vous pouvez transmettre un pointeur de fonction avec (si nécessaire) certains paramètres. Il pourrait ressembler à ceci:

typedef void (*Instruction)(int); 

void foo(int) 
{ 
    // do something 
} 

void execute_at_frame(int frame_number, Instruction ins, int param) 
{ 
    for(int f = 1 ; f < F_MAX ; f++) 
    { 
     /* other instructions */ 
     if (f == frame_number) 
      ins(param); 
    } 
} 

Exemple d'utilisation:

execute_at_frame(1000, foo, 42); 

Si vous utilisez des modèles variadique, vous pouvez le faire fonctionner avec toute signature. Exemple simplifié:

void foo(int) 
{ 
} 

float bar(int, char, double) 
{ 
    return 1.0; 
} 

template<typename F, typename... Args> 
void execute(F ins, Args... params) 
{ 
    ins(params...); 
} 

int main() 
{ 
    execute(foo, 1); 
    execute(bar, 1, 'a', 42.0); 
} 

Vous aurez besoin du compilateur C++ 11 pour cela.

+0

Oui, mais le nombre (et le type) des paramètres 'param' peuvent changer beaucoup ... ce n'est pas toujours un' int' et ce n'est pas toujours un argument: -/ – Jav

+0

@Jav J'ai mis à jour la réponse. – jrok

0

votre paramètre peut aussi être un pointeur de classe de base, le point de classe dérivée qui a une fonction virtuelle

0

code pour utiliser une fonction comme paramètre:

#include <functional> 
#include <iostream> 
using namespace std; 

int instruction(int instruc) 
{ 
    return instruc ; 
} 


template<typename F> 
void execute_at_frame(int frame, const F& function_instruction) 
{ 
    std::cout << function_instruction(frame) << '\n'; 
} 


int main() 
{ 
    execute_at_frame(20, instruction); // use reference 
    execute_at_frame(40, &instruction); // use pointer 




    cout<<" \nPress any key to continue\n"; 
    cin.ignore(); 
    cin.get(); 

    return 0; 
} 
Questions connexes