function-pointers

How to define a multiset using a function pointer?

强颜欢笑 提交于 2021-02-07 08:51:22
问题 I'm stuck when doing an exercise from C++ Primer 5th Edition ,which goes like Exercise 11.11: Redefine bookstore without using decltype. Below is the relevant codes in this book: multiset<Sales_data, decltype(compareIsbn)*> bookstore(compareIsbn); The code for class Sales_data is a little bit verbose to post here,so I wrote a simpler one and defined the multiset in the same style, as shown below. It compiled without any error. class A { int lenth; public: int getLenth() const {return lenth;}

c++ pointer to non-static member functions

百般思念 提交于 2021-02-07 02:58:06
问题 I have read many posts and answers about pointers to non-static member functions, but none looks able to solve my problem. So I have created a short example to replicate my issue here: even if this example could be "solved" in different ways, for the final software it is important to keep the structure like in the example, thanks. This is the header of the class "Funcs.h": class Funcs { private: double a = 1, b = 2; public: Funcs(); ~Funcs(); double Fun1(double X); double solver(double X0);

c++ pointer to non-static member functions

余生颓废 提交于 2021-02-07 02:57:51
问题 I have read many posts and answers about pointers to non-static member functions, but none looks able to solve my problem. So I have created a short example to replicate my issue here: even if this example could be "solved" in different ways, for the final software it is important to keep the structure like in the example, thanks. This is the header of the class "Funcs.h": class Funcs { private: double a = 1, b = 2; public: Funcs(); ~Funcs(); double Fun1(double X); double solver(double X0);

C function pointer: Can I jump to heap memory assembler code?

喜欢而已 提交于 2021-02-06 08:49:55
问题 Is it possible to create a dynamic function by allocating dynamic memory, writing some assembler opcodes to it (like 0x90 0xC2 for NOP RET ), creating a function pointer which points to that dynamic memory and execute it like a regular function from within a C program? Target should be a regular x86 Linux system. 回答1: This memory doesn't have be heap memory (see note below). Moreover, I'd think that you can't change execution permission of heap memory. On Linux, You can use the following:

C function pointer: Can I jump to heap memory assembler code?

久未见 提交于 2021-02-06 08:48:55
问题 Is it possible to create a dynamic function by allocating dynamic memory, writing some assembler opcodes to it (like 0x90 0xC2 for NOP RET ), creating a function pointer which points to that dynamic memory and execute it like a regular function from within a C program? Target should be a regular x86 Linux system. 回答1: This memory doesn't have be heap memory (see note below). Moreover, I'd think that you can't change execution permission of heap memory. On Linux, You can use the following:

Pointer to Function Pointer

会有一股神秘感。 提交于 2021-02-05 04:51:44
问题 Is it possible to create a pointer to a function pointer, i.e. int32_t (*fp[2])(void) = {test_function1, test_function_2}; // initialize a function pointer <unknown> = fp; What needs to be written in place of unknown? With "normal" arrays, I could do this: int a[2] = {0, 1}; int* p = a; Many thanks in advance. 回答1: typedef void(*func_ptr_t)(void); // a function pointer func_ptr_t* ptr_to_func_ptr; // a pointer to a function pointer - easy to read func_ptr_t arr[2]; // an array of function

branch prediction on a function pointer

ぃ、小莉子 提交于 2021-02-04 05:48:14
问题 I have a loop that is running over and over again. The logic inside that loop is dependent on the mode that the program is in. To improve performance I was thinking that an array of function pointers can be initialized, functionPtr[], so that would just call functionPtrmode that runs the right logic. The loop will stay in the same mode for many cycles (the number is unknown upfront but many thousands). The program runs on an intel x64 machine only and needs no portability. I was hoping that

branch prediction on a function pointer

旧时模样 提交于 2021-02-04 05:46:26
问题 I have a loop that is running over and over again. The logic inside that loop is dependent on the mode that the program is in. To improve performance I was thinking that an array of function pointers can be initialized, functionPtr[], so that would just call functionPtrmode that runs the right logic. The loop will stay in the same mode for many cycles (the number is unknown upfront but many thousands). The program runs on an intel x64 machine only and needs no portability. I was hoping that

QList generic join() function with template

北城以北 提交于 2021-01-29 18:17:21
问题 I am trying to make a generic join() function for QList (like join() for QStringList) in order to make a toString() function for a QList of any type. This function takes a QList, a separator and a function to dertermine how to print items. Consider this code : #include <QList> #include <QDebug> template <class T> static QString join(const QList<T> &list, const QString &separator, const std::function< QString (const T &item) > toStringFunction) { QString out; for(int i = 0; i<list.size(); i++)

Adding a standard routine to function pointer passed to a function?

主宰稳场 提交于 2021-01-29 11:10:29
问题 Please consider the following: typedef int (*callback_function)(int a, int b); class receiving_callbacks_class { public: static register_callback(int iterator, callback_function fn) { function_list[iterator] = fn; } private: static callback_function function_list[10]; } This function_list is used by a C library, so it can do call backs on certain events. I now would like to add a routine to every callback that gets registered via this function. So that it gets called like this: default