function-pointers

Function pointers/delegates in Java?

无人久伴 提交于 2019-12-18 03:19:10
问题 For my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? 回答1: What about this one? HashMap<Integer, Runnable> map = new HashMap<Integer, Runnable>(); map.put(Register.ID, new Runnable() { public void run() { functionA(); } }); map.put(NotifyMessage.ID, new Runnable() { public void run() { functionB(); } }); // ... map.get(id)

correct way to assign function pointer

一笑奈何 提交于 2019-12-18 02:43:19
问题 I'm a little confused about the correct syntax for assigning a function pointer to a variable. If I have a function foo int foo(); and I am assigning a pointer to foo to variable bar void * bar; it does not seem to matter if I use bar = foo; // or bar = &foo; It seems to me that only one of these should be correct or am I missing something? 回答1: foo and &foo values are equivalent in C and have same type. The & operator here is correct but redundant. Note that assigning a function pointer to a

correct way to assign function pointer

回眸只為那壹抹淺笑 提交于 2019-12-18 02:43:07
问题 I'm a little confused about the correct syntax for assigning a function pointer to a variable. If I have a function foo int foo(); and I am assigning a pointer to foo to variable bar void * bar; it does not seem to matter if I use bar = foo; // or bar = &foo; It seems to me that only one of these should be correct or am I missing something? 回答1: foo and &foo values are equivalent in C and have same type. The & operator here is correct but redundant. Note that assigning a function pointer to a

C++ Pointer to virtual function

霸气de小男生 提交于 2019-12-17 23:29:50
问题 If you have a struct like this one struct A { void func(); }; and a reference like this one A& a; you can get a pointer to its func method like this: someMethod(&A::func); Now what if that method is virtual and you don't know what it is at run-time? Why can't you get a pointer like this? someMethod(&a.func); Is it possible to get a pointer to that method? 回答1: Pointers to members take into account the virtuality of the functions they point at. For example: #include <iostream> struct Base {

Passing a pointer to a class member function as a parameter

只谈情不闲聊 提交于 2019-12-17 22:16:17
问题 I have written a small program where I am trying to pass a pointer to member function of a class to another function. Can you please help me and where I am going wrong..? #include<iostream> using namespace std; class test{ public: typedef void (*callback_func_ptr)(); callback_func_ptr cb_func; void get_pc(); void set_cb_ptr(void * ptr); void call_cb_func(); }; void test::get_pc(){ cout << "PC" << endl; } void test::set_cb_ptr( void *ptr){ cb_func = (test::callback_func_ptr)ptr; } void test:

C++ Math Parser with user-defined function

北城以北 提交于 2019-12-17 19:55:42
问题 I want to implement a math parser with user-defined function. There are several problems to be solved. For example, int eg(int a,int b){return a+b;} is the function I want to add to the parser. First: How to store all the functions into a container? std::map<std::string,boost::any> func_map may be a choose (by func_map["eg"]=eg ". However, It's very hard to call the function in this kind of map, for I have to use any_cast<T> to get the real function from the wrapper of boost::any . Second:

Function as array value

雨燕双飞 提交于 2019-12-17 18:43:20
问题 I can't seem to find anything of this, and was wondering if it's possible to store a function or function reference as a value for an array element. For e.g. array("someFunc" => &x(), "anotherFunc" => $this->anotherFunc()) Thanks! 回答1: You can "reference" any function. A function reference is not a reference in the sense of "address in memory" or something. It's merely the name of the function. <?php $functions = array( 'regular' => 'strlen', 'class_function' => array('ClassName',

Executing machine code in memory

走远了吗. 提交于 2019-12-17 17:33:49
问题 I'm trying to figure out how to execute machine code stored in memory. I have the following code: #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], "rb"); fseek(f, 0, SEEK_END); unsigned int len = ftell(f); fseek(f, 0, SEEK_SET); char* bin = (char*)malloc(len); fread(bin, 1, len, f); fclose(f); return ((int (*)(int, char *)) bin)(argc-1, argv[1]); } The code above compiles fine in GCC, but when I try and execute the program from the command

How to allow templated functor work on both member and non-member functions

烈酒焚心 提交于 2019-12-17 17:04:38
问题 I got this logging templated functor template<typename RetType, typename Arg1Type, typename Class> class Logger { public: RetType operator()(Arg1Type s, ...) { if(func != 0 && parser != 0) return (parser->*func)(s); else if(nfunc != 0) return nfunc(s); return RetType(); } Logger& operator=(RetType(*fun)(Arg1Type s, ...)) { func = fun; return *this; } void Bind(Class* pars, RetType(Class::*fun)(Arg1Type s,...)) { parser = pars; func = fun; nfunc = 0; } void Bind(RetType(*fun)(Arg1Type s,...))

std::function to member function

这一生的挚爱 提交于 2019-12-17 16:42:07
问题 #include <functional> struct A { int func(int x, int y) { return x+y; } }; int main() { typedef std::function<int(int, int) > Funcp; A a; //Funcp func = std:::bind(&A::func, &a); Funcp func = std::bind(&A::func, a, std::placeholders::_1); return 0; } I am getting errors in both of the above bind functions: error C2825: '_Fty': must be a class or namespace when followed by '::' Where is the syntax error? I am using visual studio 2010 回答1: Funcp func = std::bind(&A::func, &a, std::placeholders: