function-pointers

Error with address of parenthesized member function

心不动则不痛 提交于 2019-11-26 09:44:10
问题 I found something interesting. The error message says it all. What is the reason behind not allowing parentheses while taking the address of a non-static member function? I compiled it on gcc 4.3.4. #include <iostream> class myfoo{ public: int foo(int number){ return (number*10); } }; int main (int argc, char * const argv[]) { int (myfoo::*fPtr)(int) = NULL; fPtr = &(myfoo::foo); // main.cpp:14 return 0; } Error: main.cpp:14: error: ISO C++ forbids taking the address of an unqualified or

What are function pointers used for, and how would I use them?

拈花ヽ惹草 提交于 2019-11-26 09:43:48
问题 I understand I can use pointers for functions. Can someone explain why one would use them, and how? Short example code would be very helpful to me. 回答1: A simple case is like this: You have an array of operations (functions) according to your business logic. You have a hashing function that reduces an input problem to one of the business logic functions. A clean code would have an array of function pointers, and your program will deduce an index to that array from the input and call it. Here

Function pointer arrays in Fortran

对着背影说爱祢 提交于 2019-11-26 09:38:15
问题 I can create function pointers in Fortran 90, with code like real, external :: f and then use f as an argument to another function/subroutine. But what if I want an array of function pointers? In C I would just do double (*f[])(int); to create an array of functions returning double and taking an integer argument. I tried the most obvious, real, external, dimension(3) :: f but gfortran doesn\'t let me mix EXTERNAL and DIMENSION. Is there any way to do what I want? (The context for this is a

convert std::bind to function pointer

左心房为你撑大大i 提交于 2019-11-26 09:36:10
问题 I have a third-party library which has a method that takes a function pointer as the first parameter: int third_party_method(void (*func)(double*, double*, int, int, double*), ...); I want to pass a pointer to a class\' method that is declared as follows: class TestClass { public: void myFunction (double*, double*, int, int, void*); I tried to pass this function as follows: TestClass* tc = new TestClass(); using namespace std::placeholders; third_party_method(std::bind(&TestClass::myFunction,

C++ Call Pointer To Member Function

江枫思渺然 提交于 2019-11-26 09:10:01
问题 I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax? typedef void (Box::*HitTest) (int x, int y, int w, int h); for (std::list<HitTest>::const_iterator i = hitTestList.begin(); i != hitTestList.end(); ++i) { HitTest h = *i; (*h)(xPos, yPos, width, height); } Also im trying to add member functions to it here std::list<HitTest> list; for (std::list<Box*>::const_iterator i = boxList.begin(); i != boxList.end();

C++/C function pointers that return void*

倖福魔咒の 提交于 2019-11-26 08:30:39
问题 I\'m trying to call a function that takes an argument, void(*)(void*, int, const char*) , but I cannot figure out how to pass those arguments to the function. Example: void ptr(int); int function(int, int, void(*)(int)); I am trying to call the function like this: function(20, 20, ptr(20)); Is this possible? 回答1: You are doing one thing incorrectly - you are trying to invoke your 'ptr' function before invoking 'function'. What you were supposed to do is to pass just a pointer to 'ptr' and

Function pointers casting in C++

為{幸葍}努か 提交于 2019-11-26 08:10:47
问题 I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting: void *gptr = dlsym(some symbol..) ; typedef void (*fptr)(); fptr my_fptr = static_cast<fptr>(gptr) ; I have also tried reinterpret_cast but no luck, although the C cast operator seems to work.. 回答1: Converting a void* to a function pointer directly is not allowed (should not compile using any of the casts) in C++98/03. It is conditionally supported in C++0x

When is an array name or a function name &#39;converted&#39; into a pointer ? (in C)

穿精又带淫゛_ 提交于 2019-11-26 07:33:21
问题 1) Misconception : Whenever an array is declared in C language, a pointer to the first element of the array is created (the name of the array) implicitly. (Is it? I don\'t think so!) The first two lines of this page (though I am not sure about the correctness of the information) state the same. As we have seen, when we declare an array, a contiguous block of memory is allocated for the cells of the array and a pointer cell (of the appropriate type) is also allocated and initialized to point

How to hash and compare a pointer-to-member-function?

徘徊边缘 提交于 2019-11-26 06:48:45
问题 How can i hash (std::tr1::hash or boost::hash) a c++ pointer-to-member-function? Example: I have several bool (Class::*functionPointer)() (not static) that point to several diferent methods of the class Class and i need to hash those pointer-to-member-function. How can i do that? Also how can i compare (std::less) those member function pointers so i can store them in a std::set? 回答1: All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So

Using a STL map of function pointers

瘦欲@ 提交于 2019-11-26 05:25:03
问题 I developed a scripting engine that has many built-in functions, so to call any function, my code just went into an if .. else if .. else if wall checking the name but I would like to develop a more efficient solution. Should I use a hashmap with strings as keys and pointers as values? How could I do it by using an STL map? EDIT : Another point that came into my mind: of course using a map will force the compiler not to inline functions, but my inefficient approach didn\'t have any overhead