function-pointers

C++ Function pointers with unknown number of arguments

蓝咒 提交于 2019-12-21 09:20:03
问题 I need some help with C++, please! I'm writing a command parser for a small text-based game, and I've run into some problems. The parser is supposed to read and parse commands entered by the player. The most obvious and straightforward solution to this could be something like this (written in pseudo-code): command <- read input from the player if command == COMMAND1 do command1 else if command == COMMAND 2 do command2 ... I'm writing in C++, so I was thinking I could solve this by using an

Wrapping a C Library with Objective-C - Function Pointers

江枫思渺然 提交于 2019-12-21 06:19:32
问题 I'm writing a wrapper around a C library in Objective-C. The library allows me to register callback functions when certain events occur. The register_callback_handler() function takes a function pointer as one of the parameters. My question to you gurus of programming is this: How can I represent an Objective-C method call / selector as a function pointer? Would NSInvocation be something useful in this situation or too high level? Would I be better off just writing a C function that has the

C memcpy() a function

断了今生、忘了曾经 提交于 2019-12-21 05:02:05
问题 Is there any method to calculate size of a function? I have a pointer to a function and I have to copy entire function using memcpy. I have to malloc some space and know 3rd parameter of memcpy - size. I know that sizeof(function) doesn't work. Do you have any suggestions? 回答1: Functions are not first class objects in C. Which means they can't be passed to another function, they can't be returned from a function, and they can't be copied into another part of memory. A function pointer though

What is the difference between std::function and std::mem_fn

廉价感情. 提交于 2019-12-21 04:12:38
问题 I am having trouble figuring out the difference between the two function wrappers std::function and std::mem_fn. From the description, it seems to me that std::function does everything std::mem_fn does and more. In which instance would one use std::mem_fn over std::function ? 回答1: You can't really compare std::function with std::mem_fn . The former is a class template whose type you specify, and the latter is a function template with unspecified return type. There really isn't a situation in

How to define a general member function pointer

自闭症网瘾萝莉.ら 提交于 2019-12-21 04:08:47
问题 I have created a Timer class that must call a callback method when the timer has expired. Currently I have it working with normal function pointers (they are declared as void (*)(void), when the Elapsed event happens the function pointer is called. Is possible to do the same thing with a member function that has also the signature void (AnyClass::*)(void)? Thanks mates. EDIT: This code has to work on Windows and also on a real-time OS (VxWorks) so not using external libraries would be great.

Where exactly do function pointers point?

岁酱吖の 提交于 2019-12-21 03:52:48
问题 Given that all the primitive data types and objects have memory allocated, it is intuitively easy to imagine the pointers to these types. But where exactly do function pointers point to? Given that instructions are converted into machine code and reside in memory, should we consider they point to the memory location corresponding to the start of the functions instructions? We face many errors in pointers due to illegal memory access. Is it the case that errors occur when function pointers

Storing function pointer in std::function

馋奶兔 提交于 2019-12-20 20:05:00
问题 I'm trying to write a C++0x wrapper around dlopen()/dlsym() to dynamically load functions from shared objects: class DynamicLoader { public: DynamicLoader(std::string const& filename); template<class Signature> std::function<Signature> load(std::string const& functionName); private: void *itsLibraryHandle; }; DynamicLoader::DynamicLoader(std::string const& filename) { itsLibraryHandle = dlopen(filename.c_str(), RTLD_LAZY); if(!itsLibraryHandle) { /* Throw Some Error */ } } template<class

Storing function pointer in std::function

只愿长相守 提交于 2019-12-20 20:04:23
问题 I'm trying to write a C++0x wrapper around dlopen()/dlsym() to dynamically load functions from shared objects: class DynamicLoader { public: DynamicLoader(std::string const& filename); template<class Signature> std::function<Signature> load(std::string const& functionName); private: void *itsLibraryHandle; }; DynamicLoader::DynamicLoader(std::string const& filename) { itsLibraryHandle = dlopen(filename.c_str(), RTLD_LAZY); if(!itsLibraryHandle) { /* Throw Some Error */ } } template<class

how to use std::function to point to a function template

白昼怎懂夜的黑 提交于 2019-12-20 11:53:08
问题 #include <functional> int func(int x, int y) { return x+y; } int main() { typedef std::function<int(int, int)> Funcp; Funcp funcp = func; return 0; } But is it possible to point to a template function? #include <functional> template<class T> T func(T x, T y) { return x+y; } int main() { typedef std::function<?(?, ?)> Funcp; Funcp funcp = func; return 0; } 回答1: No. A template function is exactly that, a template. It's not a real function. You can point a std::function to a specific

C: Function pointer inside a typedef struct

此生再无相见时 提交于 2019-12-20 10:46:32
问题 I am trying to create a linked list in C but trying to pack it nicely in somewhat of a C++ style class. I am having some issues however using function pointers in C. typedef struct linkedList { int count; struct msgNode *front; struct msgNode *back; void (*addMSG)(unsigned char *, int, struct linkedList *); } msgList; void addMSG(unsigned char *data, int size, struct linkedList *self); Ideally, I would like to have it such that you can make you list and then to add you can simply call a