function-pointers

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

你离开我真会死。 提交于 2019-11-26 19:08:51
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? All C++ objects, including pointers to member functions, are represented in memory as an array of chars. So you could try: bool (Class::*fn_ptr)() = &Class::whatever; const char *ptrptr = static_cast<const char*>

Function pointer as an argument

自古美人都是妖i 提交于 2019-11-26 19:08:24
问题 Is it possible to pass a function pointer as an argument to a function in C? If so, how would I declare and define a function which takes a function pointer as an argument? 回答1: Definitely. void f(void (*a)()) { a(); } void test() { printf("hello world\n"); } int main() { f(&test); return 0; } 回答2: Let say you have function int func(int a, float b); So pointer to it will be int (*func_pointer)(int, float); So than you could use it like this func_pointer = func; (*func_pointer)(1, 1.0); /

What is guaranteed about the size of a function pointer?

∥☆過路亽.° 提交于 2019-11-26 18:57:56
In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures: the size of a void* is the same size as a function pointer? the size of the function pointer does not differ due to its return type? the size of the function pointer does not differ due to its parameter types? I assume the answer is yes to all of these, but I want to be sure. For context, I'm calling sizeof(struct mystruct) and nothing more. From C99 spec, section 6.2.5, paragraph 27: A pointer to void shall have the same representation and alignment

C late binding with unknown arguments

痞子三分冷 提交于 2019-11-26 18:41:13
问题 I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time. But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into. In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that

Function pointer vs Function reference

孤者浪人 提交于 2019-11-26 17:37:40
问题 In the code below, function-pointer and what i considered as "function-reference" seems to have identical semantics: #include <iostream> using std::cout; void func(int a) { cout << "Hello" << a << '\n'; } void func2(int a) { cout << "Hi" << a << '\n'; } int main() { void (& f_ref)(int) = func; void (* f_ptr)(int) = func; // what i expected to be, and is, correct: f_ref(1); (*f_ptr)(2); // what i expected to be, and is not, wrong: (*f_ref)(4); // i even added more stars here like (****f_ref)(4

C++ function pointer (class member) to non-static member function

家住魔仙堡 提交于 2019-11-26 17:31:40
class Foo { public: Foo() { do_something = &Foo::func_x; } int (Foo::*do_something)(int); // function pointer to class member function void setFunc(bool e) { do_something = e ? &Foo::func_x : &Foo::func_y; } private: int func_x(int m) { return m *= 5; } int func_y(int n) { return n *= 6; } }; int main() { Foo f; f.setFunc(false); return (f.*do_something)(5); // <- Not ok. Compile error. } How can I get this to work? The line you want is return (f.*f.do_something)(5); (That compiles -- I've tried it) " *f.do_something " refers to the pointer itself --- "f" tells us where to get the do_something

Using a STL map of function pointers

我的梦境 提交于 2019-11-26 17:26:40
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 generated by the necessity of function calls, it just executes code. So I wonder if the overhead

Passing a lambda into a function template

这一生的挚爱 提交于 2019-11-26 16:49:24
问题 I'm learning C++, and I'm trying to implement a binary search function that finds the first element for which a predicate holds. The function's first argument is a vector and the second argument is a function that evaluates the predicate for a given element. The binary search function looks like this: template <typename T> int binsearch(const std::vector<T> &ts, bool (*predicate)(T)) { ... } This works as expected if used like this: bool gte(int x) { return x >= 5; } int main(int argc, char**

C++: Calling member function via pointer

我是研究僧i 提交于 2019-11-26 16:44:59
问题 I have this example code of using pointer to member function, which I want to change during runtime, but I cannot make it work. I've already tried this->*_currentPtr(4,5) (*this)._currentPtr(4, 5) . What is the proper way of calling pointer to method inside same class ? The error : expression must have (pointer-to-) function type #include <iostream> #include <cstdlib> class A { public: void setPtr(int v); void useFoo(); private: typedef int (A::*fooPtr)(int a, int b); fooPtr _currentPtr; int

Convert C++ function pointer to c function pointer

泄露秘密 提交于 2019-11-26 16:18:19
问题 I am developing a C++ application using a C library. I have to send a pointer to function to the C library. This is my class: class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); private: Ui::MainWindow *ui; void f(int*); private slots: void on_btn_clicked(); }; This is my on_btn_clicked function: void MainWindow::on_btn_clicked() { void (MainWindow::* ptfptr) (int*) = &MainWindow::f; c_library_function(static_cast<void()(int*)>(ptfptr), NULL); }