Search code examples
c++classvariablesclass-variables

how to use function as variable c++


I want to create somekind of event system in c++ for my needs

I know that you can use fuction as other function parameter

May be i missed something(But in c# you can do it with events handlers)


Solution

  • You can store it in a pointer. For example

    void func(int a) { ... }
    void (*ptr)(int) = &func;
    (*ptr)(5); // call the function with value 5
    

    ptr is a pointer which takes as value a memory of a function that has one 'int' argument and 'void' as return type.