P
In what cases are the functions used callback?It is a question too broad to be answered in a concrete way, so it is best to give a comprehensive answer: when necessary.The design pattern https://es.wikipedia.org/wiki/Visitor_(patr%C3%B3n_de_dise%C3%B1o) can be implemented with callbacks (although it can also be implemented without them).Event response functions are often implemented through callbacks.Can they be used in classes?Sure. Like any other data, a callback can be a member of a class, having these functions:int suma (int a, int b) { return a + b; }
int resta (int a, int b) { return a - b; }
int multiplica(int a, int b) { return a * b; }
int divide (int a, int b) { return a / b; }
And this class that contains a callback:class Clase
{
int (callback*)(int, int);
public:
Clase(int (operación*)(int, int)) : callback(operación) {}
int operar(int a, int b) { return callback(a, b); }
};
We can do the following:Clase a(suma), b(resta);
std::cout << a.operar(1, 2) << '\n'; // muestra 3
std::cout << b.operar(1, 2) << '\n'; // muestra -1
Do they work similarly than in javascript?No. There are many differences between JavaScript and C++ functions:JavaScript is a language of https://es.wikipedia.org/wiki/Tipado_din%C3%A1mico .The JavaScript functions accept a number of undetermined parameters.JavaScript functions can be used as functions or as objects.
Creating a function with new, this will act like a class, the this the created object will point to itself and the return value of the function is discarded.Creating a function without new the this points to the global scope (which in browsers will be the object window) and the return value can be captured by anyone who calls the function.How does a function pass through a parameter? We've seen it two sections ago:int funcion_que_recibe_una_funcion(int (*parametro)(int, int), int a, int b)
{
return parametro(a, b);
}
Since C++ is a language https://es.wikipedia.org/wiki/Tipado_fuerte any parameter that points to a function needs to know all the types that form part of the function, i.e. the type of return and the types of parameters. Functions with different parameters and different types of return will have different types. So to create a function type variable (callback) in C++ we will do the following:tipo_de_retorno (nombre_de_la_variable)( ... lista de parametros ... );
So...// funcion que devuelve int y recibe dos int
int (a)(int, int);
// funcion que no devuelve ni recibe nada
void (b)();
// funcion que devuelve un string y recibe un puntero constante a char y un int
std::string (c)(const char *, int);
Normally this syntax is confusing, so it's usually used typedef:typedef int (i_ii)(int, int);
typedef void (v_v)();
typedef std::string (s_ci)(const char *, int);
int funcion_que_recibe_una_funcion(i_ii *parametro, int a, int b)
{
return parametro(a, b);
}
void funcion_que_recibe_una_funcion(v_v *parametro)
{
parametro();
}
std::string funcion_que_recibe_una_funcion(s_ci *parametro, const char a, int b)
{
return parametro(a, b);
}
Note that we added to indicate that we are getting a pointer to function; on the other hand typedef is often considered confusing as well, so the aliases were added from C++11:using i_ii = int(int, int);
using v_v = void();
using s_ci = std::string(const char *, int);
We can also use template aliases to make pointers even easier to function:template <typename RETORNO, typename ... PARAMETROS>
using funcion = RETORNO(PARAMETROS ...);
int funcion_que_recibe_una_funcion(funcion<int, int, int> *parametro, int a, int b)
{
return parametro(a, b);
}
void funcion_que_recibe_una_funcion(funcion<void> *parametro)
{
parametro();
}
std::string funcion_que_recibe_una_funcion(funcion<std::string, const char *, int> *parametro, const char *a, int b)
{
return parametro(a, b);
}
And we can also add the pointer directly to the typedef o alias:typedef int (*p_i_ii)(int, int);
typedef void (*p_v_v)();
typedef std::string (*p_s_ci)(const char *, int);
using ap_i_ii = int()(int, int);
using ap_v_v = void()();
using ap_s_ci = std::string(*)(const char *, int);
template <typename RETORNO, typename ... PARAMETROS>
using puntero_a_funcion = RETORNO(*)(PARAMETROS ...);
When declaring the parameters of the function What is your type?The type of function parameters does not change, a type parameter T It'll be kind of T Always.