function-pointers

How to create map of pointers to static member functions in C++11?

不羁岁月 提交于 2019-12-11 08:36:59
问题 I want to create a class that contains some static member functions and a map with pointers to these functions. They can, however, take different numbers and types of arguments. So following this thread I tried something like: class BeliefCondFunc { static std::unordered_map<std::string, std::function<bool()>> FuncMap; static bool Greater(int A, int B) { return A > B; } static bool Between(float A, float B, float C) { return A > B && A < C; } static void InitMap() { FunctionMap["Greater"] =

How can I pass a class method to another function like what happen in thread constructor

亡梦爱人 提交于 2019-12-11 08:04:42
问题 I want to pass a class method to another function and I wrote these codes: class x { executeQuery(std::string query, int (*f)(void* cpuInfo, int argc, char** argv, char** azColName)) { int rc = sqlite3_exec(db, query.c_str(), &f, 0, &errMessage); ... } }; The above code shows the function that I called from class constructor! myclass() { xObject->executeQuery("test", &(myclass::myfunction)); } And this part of code shows how I pass myfunction to that method! But, during compile time I got

boost::bind return a function object which is the argument for a function that requires pointer

六眼飞鱼酱① 提交于 2019-12-11 07:44:57
问题 I am doing C++ coding on Linux about boost::bind. The return data type of boost::bind is a function object, which is an input argument to another function bridge_set_pound_var_func. But, bridge_set_pound_var_func's input argument must be a function pointer. bridge_set_pound_var_func's interface cannot be changed. The code is as follows: #include <boost/bind.hpp> #include <iostream> using namespace boost; class myA { public: int bridge_set_pound_var_func( int (*pound_var_func)(const char *,

Function wrapper via (function object) class (variadic) template

两盒软妹~` 提交于 2019-12-11 05:54:50
问题 C++ I'm trying to implement a function wrapper via a (function object) class (variadic) template. The class has as its only data member a function pointer that is initialized by or assigned the function pointer it is wrapping. The parametrized constructor takes a function pointer and initializes the member by it. The operator() method takes argument(s) (or none) and calls the wrapped function with them. At least that's the idea. I get many errors, which I mark with comments. VC11 (with the

Difference between passing &:method and :method as function arguments in ruby

好久不见. 提交于 2019-12-11 05:40:39
问题 I'm struggling in understanding when to use the ampersand in passing symbols to functions representing a method. For example, If I wanted to calculate the sum of the range 1..10, I could do the following: (1..10).inject(:+) This originally lead me to believe that if you wanted to pass a symbol to define a method to "Magically" be used in the function, you would pass the function name as a symbol. But then I see something like this in rails: total = Product.find(product_list).sum(&:price) If I

Help with boost bind/functions

淺唱寂寞╮ 提交于 2019-12-11 05:17:31
问题 I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target sig Here's what I have so far: //somewhere else... ... registerFunction<LuaEngine>("testFunc", &LuaEngine::testFunc, this); ... //0 arg callback void funcCallback0(boost::function<void ()> func, lua_State *state) { func(); } template<typename SelfType> void registerFunction(const std::string &funcName, boost::function<void (SelfType*)> func, SelfType *self) { //funcToCall has to match lua

Pointer to a member-function

旧城冷巷雨未停 提交于 2019-12-11 04:53:14
问题 I would like to do the following: I have two classes, A and B, and want to bind a function from A to a function from B so that whenever something calls the function in B, the function from A is called. So basically, this is the scenario: ( important A and B should be independent classes) This would be class A: class A { private: // some needed variables for "doStuff" public: void doStuff(int param1, float *param2); } This is class B class B { private: void callTheFunction(); public: void

Why surround the function with parentheses?

那年仲夏 提交于 2019-12-11 03:16:31
问题 I shown some code that i did understand. following code is example code. static void (_func)(int p); int main() { .... _func(3); .... } static void (_func)(int p) { .... } Generally I know that function surrounded with parentheses is used with '*' for function pointer as (*_func), but above code why did surround the function with parentheses at declaration of function? Is there some reason to use this method ? 回答1: Putting parens around a function name will prevent it from being 'overridden'

How do I access a zero-terminated array of function pointers declared in C from Rust?

北城余情 提交于 2019-12-11 03:11:17
问题 I have the following C code with a zero-terminated array of function pointers: #include <stdio.h> void hello_register(void) { printf("hello_register called\n"); } void (*vlog_startup_routines[])() = { hello_register, 0 }; This code is compiled and linked to my Rust program using a Cargo build script. How can I call each of the function pointers in the array from Rust? 回答1: A combination of the previous two answers looks nicer: extern crate libc; type VlogStartupRoutine = Option<extern "C" fn(

How to use a typedef function pointer to register a callback

倖福魔咒の 提交于 2019-12-11 02:39:10
问题 I'm trying to implement an observer pattern (of sorts) with C++ and I want to use function pointer to do so, but I keep getting an error when trying to cast a function pointer from class B to a typedef function pointer: #include <map> typedef int (*OutputEvent)(const char*, const char*, int); class A { private: int nextListenerId; std::map<int, OutputEvent> listenerMap; public: A(){ nextListenerId = 0;} ~A(){} inline int RegisterListener(OutputEvent callback) { nextListenerId++; listenerMap