How to pass a function-pointer to a function in C ++?

Here are three functions, such as: -

float Plus (float a, float b) { return a+b; } float Minus (float a, float b) { return ab; } float Multiply(float a, float b) { return a*b; } 

now there is a function that takes a pointer to a function as one of the arguments: -

 void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float)) { float result = pt2Func(a, b); // call using function pointer cout << " Result = "; // display result cout << result << endl; } 

and to call the above function "Function_Pointer_func" the function is written below

 void Replace() { Function_Pointer_func(2, 5, /* pointer to function 'Minus' */ Plus);//// (1) Function_Pointer_func(2, 5, /* pointer to function 'Minus' */ &Minus);//// (2) } 

Why the function above works fine, since the Function_Pointer_func function takes a function argument as an argument. And if we replace RHS in line

  float result = pt2Func(a, b); // call using function pointer 

function "Function_Pointer_func" (* pt2Func) (a, b), and then it works, but for (& pt2Func) (a, b);

it gives an error in VS2008:

"C2064: the term does not evaluate a function that takes 2 arguments"

Now replace the argument "float (* pt2Func) (float, float)" in the function "Function_Pointer_func" with float (pt2Func) (float, float), and then all three

 float result = pt2Func(a, b); // float result = (&pt2Func)(a, b); // float result = (*pt2Func)(a, b); // 
The task

works why? Hopefully the reason for my discomfort is understanding the basic understanding of a pointer function. Well, I can not imagine Q? without any good reading, but yes, I have not done any intensive research on this subject, so please feel free to recommend some readings in this regard that would clear up my ambiguity.

Thanks for the help in advance.

+4
source share
2 answers

Functions automatically break up into function pointers. In this context

  • function_name really means &function_name if not specified.

  • &function_name turns a function into a function pointer.

  • *function_name really means *(function_name) , which gets higher than *(&function_name) . * and & "cancel", so to speak, and the resulting function_name again returned to &function_name .

+2
source

This is the C ++ standard.

 float Plus(float a, float b); void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float)); Function_Pointer_func(2, 5, Plus); // (1) ... float result = pt2Func(a, b); // (2) 

(1) - conversion of a function to a pointer ( standard 2003, 4.3 ):

 An lvalue of function type T can be converted to an rvalue of type "pointer to T." The result is a pointer to the function 

(2) - function call ( standard 2003, 5.2.2 ):

 For an ordinary function call, the postfix expression shall be either an lvalue that refers to a function (in which case the function-to-pointer standard conversion (4.3) is suppressed on the postfix expression), or it shall have pointer to function type. 

[UPDATE] More details:

 void Replace() { Function_Pointer_func(2, 5, Plus); Function_Pointer_func(2, 5, &Minus); } 

Minus is function => & Minus is a pointer to a function, so no conversion, the 3rd argument of Function_Pointer_func is perfect. Plus, it is a function, so to match the Function_Pointer_func function, it must be converted to a pointer. Standard (1) says that this can be done automatically.

Challenges:

 void Function_Pointer_func(float a, float b, float (*pt2Func)(float, float)) { float result = pt2Func(a, b); // call by pointer, see (2) float result = (*pt2Func)(a, b); // convert pointer to function, so 'normal' call float result = (&pt2Func)(a, b); // pointer to function pointer, nope, will not work } 
+2
source

Source: https://habr.com/ru/post/1496638/


All Articles