#include <iostream>
typedef char (*callback)(int *data);
typedef char (callback2)(int *data);
typedef char callback3(int *data);
char fun_a(int *d)
{
if (*d == 10)
return 'A';
else
return 'B';
}
int main()
{
int num = 10;
callback cb_1_a;
cb_1_a = fun_a;
std::cout << cb_1_a(&num) << std::endl;
callback cb_1_b;
cb_1_b = &fun_a;
std::cout << cb_1_b(&num) << std::endl;
callback cb_1_c;
cb_1_c = &fun_a;
std::cout << (*cb_1_c)(&num) << std::endl;
/*
callback2 cb2;
cb2 = fun_a; // wrong
callback3 cb3;
cb3 = fun_a; // wrong
*/
return 0;
}
C++ compiler doesn't complain about the typedef of callback2
and callback3
.
Question> What do typedef char (callback2)(int *data)
and typedef char (callback3)(int *data)
mean? Is there a use case where I can apply them?
A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time [Source : Wiki ]. In simple language, If a reference of a function is passed to another function as an argument to call it, then it will be called as a Callback function.
To use it we create a variable of the created type and assign it a pointer to one of the functions in question: Then to call the function pointed to by the function pointer variable: Thus the typedef allows a simpler syntax when dealing with function pointers.
We can use typedef to simplify the usage of function pointers. Imagine we have some functions, all having the same signature, that use their argument to print out something in different ways: Now we can use a typedef to create a named function pointer type called printer:
Although using typedef names for pointer to function types makes life easier, it can also lead to confusion for others who will maintain your code later on, so use with caution and proper documentation. See also Function Pointers.
Your first typedef
defines callback
as a pointer to a function that takes an int*
argument and returns a char
value. So, being a pointer, you can assign a value (the address of an appropriate function) to variables of that type, as you do in the case of your cb_1_a
, cb_1_b
and cb_1_c
variables; in the first case (cb_1_a = fun_a;
), you are using the function's name (but note: without the parentheses) in place of using an explicit &
operator (if you add the parentheses after a function name, then your are invoking that function).
Your second and third typedef
statements define their types as actual functions, and you cannot assign a value to a function. Also, as you have nothing other than the name callback2
inside the first set of parentheses in typedef char (callback2)(int* data);
, those parentheses are actually redundant, and the type is equivalent to that defined in the next line: typedef char callback3(int* data);
.
However, you can use the second two types (indirectly) to declare function pointers, by adding the *
in the relevant variable declarations, like this:
//...
callback2 *cb2; // cb2 is now a POINTER to a "callback2" type!
cb2 = fun_a; // So this is OK - We assign the ADDRESS of fun_a to the cb2 pointer
callback3 *cb3; // And the same here...
cb3 = fun_a; // ...and here
//...
Feel free to ask for further clarification and/or explanation. Also, you may find this post helpful, in terms of using typedef
and function pointer syntax: Typedef function pointer?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With