function-pointers

c++ std::map of heterogeneous function pointers

落花浮王杯 提交于 2019-12-11 02:17:53
问题 Is it possible to store pointers to various heterogenous functions like: In the header: int functionA (int param1); void functionB (void); Basically this would the part I don't know how to write: typedef ??boost::function<void(void)>?? functionPointer; And afterwards: map<char*,functionPointer> _myMap; In the .cpp void CreateFunctionMap() { _myMap["functionA"] = &functionA; _myMap["functionB"] = &functionB; ... } And then reuse it like: void execute(int argc, char* argv[]) { if(argc>1){ int

Conditional Function definition

只愿长相守 提交于 2019-12-11 02:07:38
问题 I have a code with a very long while loop. In this while loop, there is a long switch-case function in order to know which function should be applied on bar . int i=0; while(i<BigNumber) { switch(variable) { case 1: foo1(bar); case 2: foo2(bar); etc... } i = i+1; } However, from one iteration to another of this while loop, the case in the switch-case structure is always the same. I would therefore, think that it would be more intelligent to define a unique function foo to apply on bar before

list/vector of class methods in a class variable

佐手、 提交于 2019-12-11 02:01:53
问题 I need to maintain a list of methods that will be executed in different orders for testing. We are moving away from C to C++ to use google framework. Is it possible to maintain a list of functions pointers to some of the class methods to be used for execution inside the class, so that they can be used after instantiation ? please refer http://cpp.sh/265y #include <iostream> #include <string> #include <vector> using namespace std; typedef void (*funcType)(); class Sample { public: vector

unresolved overloaded function type

怎甘沉沦 提交于 2019-12-11 01:59:57
问题 I'm trying to write a templated class that holds a reference to an object of it's template parameter type, and a pointer to a void returning no arg member function of that class. However I'm getting an error about '<unresolved function type>' when I compile. template<class T> class memberAction { public: memberAction(void (T::*func)() , T& object); private: void (T::*m_func)(); T& m_object; }; template<class T> memberAction<T>::memberAction(void (T::*func)() , T& object) { m_func = func; m

Fortran Functions with a pointer result in a normal assignment

大憨熊 提交于 2019-12-11 01:23:26
问题 After some discussion on the question found here Correct execution of Final routine in Fortran I thought it will be useful to know when a function with a pointer result is appropriate to use with a normal or a pointer assignment. For example, given this simple function function pointer_result(this) implicit none type(test_type),intent(in) pointer :: this type(test_type), pointer :: pointer_result allocate(pointer_result) end function I would normally do test=>pointer_result(test) , where test

Random shuffle and function pointers

。_饼干妹妹 提交于 2019-12-10 23:09:08
问题 I have the following function, which generates an integer in a given range: 18 int choosecard(){ 19 boost::uniform_int<> dist(0,51); 20 boost::variate_generator< boost::mt19937&, boost::uniform_int<> > cardchoice(gen, dist); 21 return cardchoice(); 22 } I want to use it as an argument to std::random_shuffle, so that I can shuffle a vector of 52 integers. 64 int (*pchoosecard)(void) = choosecard; 65 66 std::random_shuffle(cards.begin(), cards.end(), pchoosecard); However I get this error that

Pass pointer array to function

↘锁芯ラ 提交于 2019-12-10 22:45:49
问题 I am trying to pass a file pointer array to a function (not sure about the terminology). Could anyone please explain the proper way to send 'in[2]'? Thank you. #include<stdio.h> #include<stdlib.h> void openfiles (FILE **in[], FILE **out) { *in[0] = fopen("in0", "r"); *in[1] = fopen("in1", "r"); *out = fopen("out", "w"); } void main() { FILE *in[2], *out; openfiles (&in, &out); fprintf(out, "Testing..."); exit(0); } 回答1: Try: void openfiles (FILE *in[], FILE **out) { in[0] = fopen("in0", "r");

Implementing callback with pointer to non-static member function

点点圈 提交于 2019-12-10 21:45:56
问题 Let's say I'm developing a grocery list manager. I have a window with a GroceryListDisplay , which is a control that displays the items that are on the grocery list. The grocery data is stored by the Model component of the program, in the GroceryStorage class. To load a saved file into my program, the Model component of my program has to be repopulated with data that was imported from the file. The View component will need to be notified of this new data, otherwise the GUI won't be updated

Function pointer (to other kernel) as kernel arg in CUDA

大城市里の小女人 提交于 2019-12-10 21:24:18
问题 With dynamic parallelism in CUDA, you can launch kernels on the GPU side, starting from a certain version. I have a wrapper function that takes a pointer to the kernel I want to use, and it either does this on the CPU for older devices, or on the GPU for newer devices. For the fallback path it's fine, for the GPU it's not and says the memory alignment is incorrect. Is there a way to do this in CUDA (7)? Are there some lower-level calls that will give me a pointer address that's correct on the

c++ pointers to overloaded functions

耗尽温柔 提交于 2019-12-10 20:32:10
问题 I'm trying to expose a overloaded function using boost::python. the function prototypes are: #define FMS_lvl2_DLL_API __declspec(dllexport) void FMS_lvl2_DLL_API write(const char *key, const char* data); void FMS_lvl2_DLL_API write(string& key, const char* data); void FMS_lvl2_DLL_API write(int key, const char *data); I'v seen this answer: How do I specify a pointer to an overloaded function? doing this: BOOST_PYTHON_MODULE(python_bridge) { class_<FMS_logic::logical_file, boost::noncopyable>(