I need to pass function with no arguments to another function, how can I do it?
I googled a bit, and found out that I need to use std::function
from functional
, but didn't understand how to pass function without arguments.
I need to pass function with no arguments to another function, how can I do it?
I googled a bit, and found out that I need to use std::function
from functional
, but didn't understand how to pass function without arguments.
It depends on the type of function you want to call. You cannot pass a function that is part of a class, at least not directly. If you want to pass a normal function you can just pass the function as a pointer (its memory address). For a class function, you can use a lambda to do something similar.
void TheCallback() {
printf("The Callback has been called!");
}
using TheCallbackT = void(*)();
void DoTheFunction(int count, const TheCallbackT& func) {
for (int i=0; i<count; i++) {
func();
}
}
// using templates
// you can use a lambda to call a class function here
template<typename Function>
void DoTheFunctionT(int count, const Function& func) {
for(int i=0; i<count; i++) {
func();
}
}
// C compatible
void DoTheFunctionC(int count, void(*func)(void*), void* _this) {
for(int i=0; i<count; i++) {
func(_this);
}
}
// for a struct/class function with no params
// it looks like no parameters, but the struct pointer itself may be the first/only param
// it may be pushed onto the stack
struct TheStruct {
int a,b,c;
int GetTotal() {
return a+b+c;
}
};
void PrintStructTotal(void* ctx) {
TheStruct* pTheStruct = (TheStruct*)ctx;
printf("TheTotalC: %i\n", pTheStruct->GetTotal());
}
int main() {
TheStruct powerfulobj;
powerfulobj.a=2;
powerfulobj.b=3;
powerfulobj.c=6;
// for normal function
DoTheFunction(10, TheCallback);
// for class function
DoTheFunctionT(10, [&](){ printf("TheTotalT: %i\n", powerfulobj.GetTotal()); });
// C
DoTheFunctionC(12, PrintStructTotal, &powerfulobj);
// mixed
DoTheFunctionC(12,
[](void* ptr) {
printf("TheTotalCM: %i\n", ((TheStruct*)ptr)->GetTotal());
},
&powerfulobj
);
return 0;
}
Using std::function
you can achieve that like in the following example: c++ shell
#include <iostream>
#include <functional>
void myFunction() {
std::cout << "Hello world!\n";
}
void foo(std::function<void()> f) {
f();
}
int main() {
foo(myFunction); // Pass the function without arguments
return 0;
}
If you dont want to use std::functional
you can use function pointers, which also work in C.
You could have something like this:
#include <stdio.h>
void sayHello() {
printf("Hello World\n");
}
void loop(unsigned int count, void(*func)()) {
for (unsigned int i = 0; i < count; i++)
func();
}
void main()
{
loop(2, sayHello);
}
In the function loop()
, the second parameter is a function pointer. The type of it is the same, as the return type from the function you want to call, and in the parentheses you would write whatever types the function you are calling has parameters. So if my function sayHello()
would actually be int sayHello(double value)
, you would have to specify the loop()
function like this:
void loop(unsigned int count, int (*func)(double);
and you would call the sayHello()
function in it with e.g. func(3.14);
. You can use the returned value of the sayHello()
as usual.