0

in PHP you can create a function as a parameter for an another function.

Example :

function example($stackoverflow) {
    $stackoverflow();
}

example(function() {
    echo "Hi StackOverflow !";
}

I would like to do this in C++

void Window::clear()
{
    EnumChildWindows(g_hWnd, {}, NULL);
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490

5 Answers5

4

The corresponding functionality in C++ is available through function objects std::function:

void call(function<void()> f) {
    f();
}

The argument that you pass could be a named function, e.g.

void test() {
    cout << "test" << endl;
}
...
call(test);

or a lambda function, e.g.

call([]() { cout << "lambda" << endl; });

Demo.

I would like to do this in C++ with a function from the WinAPI.

Another way of passing a function is with function pointers, which is the approach used by WinAPI. You cannot do it inline, because the API does not take std::function, so you have to declare a named function for use in the call:

BOOL CALLBACK CheckWindow(HWND hwnd, LPARAM lParam) {
    ... // Your processing code goes here
    return TRUE;
}
...
EnumChildWindows(g_hWnd, CheckWindow, NULL);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you, you just help me for another problem, I edited the post. :D –  Apr 16 '18 at 19:27
3

A pointer to function?

void func(void (*another_func)(void))
{
    another_func();
}

void func2(void)
{
    cout << "Hi StackOverflow!" << endl;
}

func(func2);
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Thank you, this work well for my another problem ! I edited the post ! –  Apr 16 '18 at 19:28
1

Besides passing (named) functions or (unnamed) lambdas as shown by Keine Lust and dasblinkenlight, in the realm of object orientation, you probably should revisit your code design. Usually, in OOP, function pointers are often obsolete and replaced by a design based on inheritance and function overriding. See the following short program indicating what that could mean:

struct Printable {
    virtual void print() const = 0;

};

struct ObjType1 : Printable {
    void print() const override { cout << "Hello SO!" << endl; }
};

struct ObjType2 : Printable {
    void print() const override { cout << "Hello object orientation!" << endl; }
};

void callPrint(Printable &p) {
    p.print();
}

int main() {

    ObjType1 objT1;
    ObjType2 objT2;

    callPrint(objT1);
    callPrint(objT2);
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Very interesting, thank you ! Is it possible to declare function inside callPrint, Like PHP ? –  Apr 16 '18 at 19:43
0

A pure object-oriented solution, might be useful to keep your code clear and cross-platform, inspired by Java's interface:

//Define an C++ interface
class MyInterface 
{
public:
    virtual void func2(void) = 0;
}
//-------------------------------
//Define the function with parameter as interface
void func(MyInterface * pImplementation)
{
    pImplementation->func2();   //Call interface function
}

//-------------------------------
class MyImplementation : public MyInterface
{
public:
    printf("Do something ...\n");
} myImpl;

func(&myImpl);  //Call function with interface implementation
Hongkun Wang
  • 717
  • 8
  • 22
0

It's not possible to do this in C++
Create a new function instead.