function-pointers

Why do we use std::function in C++ rather than the original C function pointer? [duplicate]

大城市里の小女人 提交于 2019-11-26 15:45:24
问题 This question already has answers here : Should I use std::function or a function pointer in C++? (5 answers) Closed 2 years ago . What is the advantage of std::function<T1(T2)> over the original T1 (*)(T2) ? 回答1: std::function can hold more than function pointers, namely functors . #include <functional> void foo(double){} struct foo_functor{ void operator()(float) const{} }; int main(){ std::function<void(int)> f1(foo), f2((foo_functor())); f1(5); f2(6); } Live example on Ideone. As the

Callback functions in Java

吃可爱长大的小学妹 提交于 2019-11-26 15:34:44
Is there a way to pass a call back function in a Java method? The behavior I'm trying to mimic is a .Net Delegate being passed to a function. I've seen people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things. If you mean somthing like .NET anonymous delegate, I think Java's anonymous class can be used as well. public class Main { public interface Visitor{ int doJob(int a, int b); } public static void main(String[] args) { Visitor adder = new Visitor(){ public int doJob(int a, int b) { return a + b; } };

Why is using the function name as a function pointer equivalent to applying the address-of operator to the function name?

折月煮酒 提交于 2019-11-26 15:22:16
It's interesting that using the function name as a function pointer is equivalent to applying the address-of operator to the function name ! Here's the example. typedef bool (*FunType)(int); bool f(int); int main() { FunType a = f; FunType b = &a; // Sure, here's an error. FunType c = &f; // This is not an error, though. // It's equivalent to the statement without "&". // So we have c equals a. return 0; } Using the name is something we already know in array. But you can't write something like int a[2]; int * b = &a; // Error! It seems not consistent with other parts of the language. What's

Python function pointer

≯℡__Kan透↙ 提交于 2019-11-26 15:11:30
I have a function name stored in a variable like this: myvar = 'mypackage.mymodule.myfunction' and I now want to call myfunction like this myvar(parameter1, parameter2) What's the easiest way to achieve this? funcdict = { 'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction, .... } funcdict[myvar](parameter1, parameter2) It's much nicer to be able to just store the function itself, since they're first-class objects in python. import mypackage myfunc = mypackage.mymodule.myfunction myfunc(parameter1, parameter2) But, if you have to import the package dynamically, then you can achieve

C syntax for functions returning function pointers

一笑奈何 提交于 2019-11-26 15:02:50
问题 Consider the following typedefs : typedef int (*f1)(float); typedef f1 (*f2)(double); typedef f2 (*f3)(int); f2 is a function that returns a function pointer. The same with f3 , but the type of the function, the pointer to which f3 returns, is f2 . How can I define f3 without the typedefs? I know typedefs are the cleaner and easier to understand way to define f3 . However, my intention here is to understand C syntax better. 回答1: Start with your declaration for f1 : int (*f1)(float); You want

C isn&#39;t that hard: void ( *( *f[] ) () ) ()

北城余情 提交于 2019-11-26 14:58:05
问题 I just saw a picture today and think I'd appreciate explanations. So here is the picture: I found this confusing and wondered if such codes are ever practical. I googled the picture and found another picture in this reddit entry, and here is that picture: So this "reading spirally" is something valid? Is this how C compilers parse? It'd be great if there are simpler explanations for this weird code. Apart from all, can these kind of codes be useful? If so, where and when? There is a question

& operator optional in function pointer assignment

好久不见. 提交于 2019-11-26 14:48:51
问题 In the following code: /* mylog.c */ #include <stdio.h> #include <stdlib.h> /* for atoi(3) */ int mylog10(int n) { int log = 0; while (n > 0) { log++; n /= 10; } return log; } int mylog2(int n) { int log = 0; while (n > 0) { log++; n >>= 1; } return log; } int main(int argc, const char* argv[]) { int (*logfunc)(int); /* function pointer */ int n = 0, log; if (argc > 1) { n = atoi(argv[1]); } logfunc = &mylog10; /* is unary '&' operator needed? */ log = logfunc(n); printf("%d\n", log); return

How to print the address of a function?

你离开我真会死。 提交于 2019-11-26 14:35:33
问题 I let gcc compile the following example using -Wall -pedantic : #include <stdio.h> int main(void) { printf("main: %p\n", main); /* line 5 */ printf("main: %p\n", (void*) main); /* line 6 */ return 0; } I get: main.c:5: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int (*)()’ main.c:6: warning: ISO C forbids conversion of function pointer to object pointer type Line 5 made my change the code like in line 6. What am I missing to remove the warning when printing a function

About Pointers To Functions in function declarations

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 14:27:16
问题 #include<stdio.h> #include<stdlib.h> int fun1() { printf("I am fun1."); return 0; } int fun2(int fun()) { fun(); return 0; } int main() { fun2(fun1); return 0; } The above program can run. As far as I am concerned, I can understand int fun2(int (*fun)()) , but I do not know how int fun2(int fun()) works. Thank you. 回答1: When you write int fun2(int fun()) , the parameter int fun() converts into int (*fun)() , it becomes exactly equivalent to this: int fun2(int (*fun)()); A more famiiar

How to print member function address in C++

白昼怎懂夜的黑 提交于 2019-11-26 14:00:31
问题 It looks like std::cout can't print member function's address, for example: #include <iostream> using std::cout; using std::endl; class TestClass { void MyFunc(void); public: void PrintMyFuncAddress(void); }; void TestClass::MyFunc(void) { return; } void TestClass::PrintMyFuncAddress(void) { printf("%p\n", &TestClass::MyFunc); cout << &TestClass::MyFunc << endl; } int main(void) { TestClass a; a.PrintMyFuncAddress(); return EXIT_SUCCESS; } the result is something like this: 003111DB 1 How can