function-pointers

Using generic std::function objects with member functions in one class

非 Y 不嫁゛ 提交于 2019-11-26 01:27:35
问题 For one class I want to store some function pointers to member functions of the same class in one map storing std::function objects. But I fail right at the beginning with this code: class Foo { public: void doSomething() {} void bindFunction() { // ERROR std::function<void(void)> f = &Foo::doSomething; } }; I receive error C2064: term does not evaluate to a function taking 0 arguments in xxcallobj combined with some weird template instantiation errors. Currently I am working on Windows 8

setTimeout calls function immediately instead of after delay

孤人 提交于 2019-11-26 01:23:26
问题 I want to make a value on an HTML page that will be updated every 5 seconds so as to not overwhelm the server. It turns out that setTimeout() inside my function is not delaying properly, but is instead being called immediately. Can someone help me find a clue? I really don\'t want to give my server too much work because I have to implement a lot more AJAX. Here\'s the code: window.onload = function GetUsersNumber() { aside = document.getElementById(\"users\"); if (XMLHttpRequest) var x = new

Function pointers and address of a function

烂漫一生 提交于 2019-11-26 01:14:38
问题 So I figured when making function pointers, you do not need the operator & to get the address of the initial function: #include <stdio.h> double foo (double x){ return x*x; } int main () { double (*fun1)(double) = &foo; double (*fun2)(double) = foo; printf(\"%f\\n\",fun1(10)); printf(\"%f\\n\",fun2(10)); printf(\"fun1 = %p \\t &foo = %p\\n\",fun1, &foo); printf(\"fun2 = %p \\t foo = %p\\n\",fun2, foo); int a[10]; printf(\" a = %p \\n &a = %p \\n\",a,&a); return 0; } output: >./a.out 100

Function Pointers in Java

泪湿孤枕 提交于 2019-11-26 01:06:03
问题 This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java? Given that pointers are somewhat absent, what is the best way about this? And to be clear, we\'re talking first class here. 回答1: The Java idiom for function-pointer-like functionality is an an anonymous class implementing an interface, e.g.

How can I pass a member function where a free function is expected?

感情迁移 提交于 2019-11-26 00:46:21
问题 The question is the following: consider this piece of code: #include <iostream> class aClass { public: void aTest(int a, int b) { printf(\"%d + %d = %d\", a, b, a + b); } }; void function1(void (*function)(int, int)) { function(1, 1); } void test(int a,int b) { printf(\"%d - %d = %d\", a , b , a - b); } int main (int argc, const char* argv[]) { aClass a(); function1(&test); function1(&aClass::aTest); // <-- How should I point to a\'s aClass::test function? return 0; } How can I use the a \'s

What&#39;s the nearest substitute for a function pointer in Java?

久未见 提交于 2019-11-26 00:20:22
问题 I have a method that\'s about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that\'s going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn\'t have function pointers. What\'s my best alternative? 回答1: Anonymous inner class Say you want to have a function passed in with a String param that returns an int . First you have to define an interface

Function Pointers in Java

六眼飞鱼酱① 提交于 2019-11-26 00:13:18
This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java? Given that pointers are somewhat absent, what is the best way about this? And to be clear, we're talking first class here. The Java idiom for function-pointer-like functionality is an an anonymous class implementing an interface, e.g. Collections.sort(list, new Comparator<MyClass>(){ public int compare(MyClass a, MyClass b) { // compare objects } });

Why do function pointer definitions work with any number of ampersands &#39;&&#39; or asterisks &#39;*&#39;?

喜欢而已 提交于 2019-11-25 23:58:34
问题 Why do the following work? void foo() { cout << \"Foo to you too!\\n\"; }; int main() { void (*p1_foo)() = foo; void (*p2_foo)() = *foo; void (*p3_foo)() = &foo; void (*p4_foo)() = *&foo; void (*p5_foo)() = &*foo; void (*p6_foo)() = **foo; void (*p7_foo)() = **********************foo; (*p1_foo)(); (*p2_foo)(); (*p3_foo)(); (*p4_foo)(); (*p5_foo)(); (*p6_foo)(); (*p7_foo)(); } 回答1: There are a few pieces to this that allow all of these combinations of operators to work the same way. The

Understanding typedefs for function pointers in C

心不动则不痛 提交于 2019-11-25 23:39:19
问题 I have always been a bit stumped when I read other peoples\' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around to such a definition while trying to understand a numerical algorithm written in C a while ago. So, could you share your tips and thoughts on how to write good typedefs for pointers to functions (Do\'s and Do not\'s), as to why are they useful and how to understand others\' work? Thanks! 回答1: Consider the signal()

How can I pass a class member function as a callback?

不问归期 提交于 2019-11-25 23:21:36
问题 I\'m using an API that requires me to pass a function pointer as a callback. I\'m trying to use this API from my class but I\'m getting compilation errors. Here is what I did from my constructor: m_cRedundencyManager->Init(this->RedundencyManagerCallBack); This doesn\'t compile - I get the following error: Error 8 error C3867: \'CLoggersInfra::RedundencyManagerCallBack\': function call missing argument list; use \'&CLoggersInfra::RedundencyManagerCallBack\' to create a pointer to member I