Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand the syntax of typedef of C function callback

Tags:

c++

#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?

like image 860
q0987 Avatar asked Apr 27 '20 19:04

q0987


People also ask

What is a callback in C++?

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.

How do you use a typedef to call a 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.

Why do we use typedef in C++?

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:

Should I use typedef names for pointer to function types?

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.


1 Answers

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?

like image 174
Adrian Mole Avatar answered Sep 17 '22 22:09

Adrian Mole