Can you pass as parameters a function in another function in c ++?
You can.
Proposal.
Apparently the function you want to execute should be called cumplir_primera_condicion
and get a matriz_juego
(which looks like a two-dimensional array of something) and 7 additional parameters that appear to be numbers and return an integer.
C ++ is a strong typing language and static , this implies (among other things) that everything (including functions) has an underlying type, the types of C ++ functions follow the following format: retorno([parámetros...])
, so the type of your function could look like this:
int(char **, int, int, int, int, int, int, int)
To make it a function pointer you have to add the name after the return type:
int(*puntero_a_funcion)(char **, int, int, int, int, int, int, int)
And so it can be used:
int cumplir_primera_condicion(char **, int, int, int, int, int, int, int)
{
// Hacer cosas
return 0;
}
void funcion(bool(*puntero_a_funcion)(char **, int, int, int, int, int, int, int)) {
if ((matriz_juego[i][j] == *casilla2 && matriz_juego[i][j-1] == *casilla2 && matriz_juego[i][j+1] == *casilla2)) {
int d = puntero_a_funcion(matriz_juego,caracteres,puntaje, aleatorio.numero,aleatorio.numero2,aleatorio.numero3,i,j);
if(j == 0){
puntaje = d - 1;
}
if(j!= 0){
puntaje = d;
}
}
}
// Llamar
funcion(cumplir_primera_condicion);
You can see that writing this is very cumbersome, so it is usually advised to use aliases of types:
int cumplir_primera_condicion(char **, int, int, int, int, int, int, int)
{
// Hacer cosas
return 0;
}
// Alias del tipo
using condicion_t = bool(char **, int, int, int, int, int, int, int)
// Llamada mas clara
void funcion(condicion_t *puntero_a_funcion) {
...
...
}
// Llamar
funcion(cumplir_primera_condicion);