Why do we need to call these functions at runtime using function pointers. we can also call them directly

After reading a little about function pointers and callbacks, I don’t understand its main purpose. For me it just looks like calling a function directly, we use a pointer to that function to call it. Can someone explain me callbacks and function pointers? How does the callback happen when we use function pointers because it seems like we are just calling a function through a pointer to it instead of directly calling it?

thank

ps: Some questions were asked here regarding callbacks and function pointers, but they don't explain my problem enough.

+5
source share
4 answers

What is the Callbak feature?
  In simple words, the Callback function is a function that is not explicitly called by the programmer. Instead, there is some mechanism that is constantly waiting for events, and it will call the selected functions in response to certain events.
  This mechanism is usually used when an operation (function) can take a long time to execute, and the calling function object does not want to wait for the operation to complete, but wants to be informed about the results of the operation. As a rule, callback functions help to implement such an asynchronous mechanism., , , , .

:
Windows:
Windows , (, , , ) . , , , () , , . .

:

//warning:  Mind compiled code, intended to illustrate the mechanism    
#include <map>

typedef void (*Callback)();
std::map<int, Callback>  callback_map;

void RegisterCallback(int event, Callback function)
{
    callback_map[event] = function;
}

bool finished = false;

int GetNextEvent()
{
    static int i = 0;
    ++i;
    if (i == 5) finished = false;
}

void EventProcessor()
{
    int event;
    while (!finished)
    {
        event = GetNextEvent();
        std::map<int, Callback>::const_iterator it = callback_map.find(event);
        if (it != callback_map.end())    // if a callback is registered for event
        {
            Callback function = *it;
            if (function)   
            {
                (*function)();
            }
            else
            {
               std::cout << "No callback found\n";
            }
        }
    }
}

void Cat()
{
   std::cout << "Cat\n";
}

void Dog()
{
    std::cout << "Dog\n";
}

void Bird()
{
    std::cout << "Bird\n";
}

int main()
{
    RegisterCallBack(1, Cat);
    RegisterCallback(2, Dog);
    RegisterCallback(3, Cat);
    RegisterCallback(4, Bird);
    RegisterCallback(5, Cat);

    EventProcessor(); 
    return 0;
}

:

Cat  
Dog   
Cat  
Bird  
Cat  

, !

: ,

+6

, , , , ( ) ! - ; qsort() , , , , .

, , - . , , - . , ! , ​​, - .

; , , !

typedef int (*myfp)();  // function pointer type

const char * libname = get_library_name_from_user();
const char * funname = get_function_name_from_user();

void * libhandle = dlopen(libname, RTLD_NOW);  // load the library
myfp fun = (myfp) dlsym(libhandle, funname);   // get our mystery function...

const int result = myfp();                     // ... and call the function
                                               // -- we have no idea which one!

printf("Your function \"%s:%s\" returns %i.\n", libname, funname, result);
+4

. sqlite3_exec() - , . SQLite , , , .

SQLite . SQLite , , .

+1

. 2 , , , sort_criteria, , 2 ?

It quickly gets complicated after all ifs and switches in a function sort_criteria, with callbacks you can point your own function (with their beautiful interpretive name) to these sorting functions.

+1
source

All Articles