I'm asking some specific questions.
For question number 2 here is what I mean:
void s(void) {
//...
}
void f(function) { // what should I put as type to pass a function as an argument
//...
}
f(s);
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.
1) Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function's name can also be used to get functions' address.
Function Pointer Syntax void (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory.
To define a function pointer, use the following syntax:
return_type (*ref_name) (type args, ...)
So, to define a function reference named "doSomething", which returns an int
and takes in an int
argument, you'd write this:
int (*doSomething)(int number);
You can then assign the reference to an actual function like this:
int someFunction(int argument) {
printf("%i", argument);
}
doSomething = &someFunction;
Once that's done, you can then invoke it directly:
doSomething(5); //prints 5
Because function pointers are essentially just pointers, you can indeed use them as instance variables in your classes.
When accepting function pointers as arguments, I prefer to use a typedef
instead of using the cluttered syntax in the function prototype:
typedef int (*FunctionAcceptingAndReturningInt)(int argument);
You can then use this newly defined type as the type of the argument for the function:
void invokeFunction(int func_argument, FunctionAcceptingAndReturningInt func) {
int result = func(func_argument);
printf("%i", result);
}
int timesFive(int arg) {
return arg * 5;
}
invokeFunction(10, ×Five); //prints 50
it is not the strict answer , but to include configurable/assignable code as a class member, I would mention using a class/struct with the operator() . For example:
struct mycode
{
int k;
mycode(int k_) : k(k_)
{
}
int operator()(int x)
{
return x*k;
}
};
class Foo
{
public : Foo(int k) : f(k) {}
public : mycode f;
};
You can do:
Foo code(5);
std::cout << code.f(2) << std::endl;
it will print '10' , it I wrote everything ok.
You have to declare f
in this way:
void f(void (*x)())
{
x(); // call the function that you pass as parameter (Ex. s()).
}
here is an excellent tutorial about function pointers and callbacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With