function-pointers

Does the address of a function change per runtime [duplicate]

北城以北 提交于 2020-01-16 01:01:15
问题 This question already has answers here : Why does the address of a function change with every run? (4 answers) Closed 4 days ago . I'm writing something in c++ where I want to read text from a file that indicates correlations between Strings and Functions I have declared in my program. For example the file could read: sin:sin PI:getPi +:add I wanted the code to take this and create a hash table or vector of String and Function Pointer data structures. Unfortunately, I realize that code would

STL iterator for MFC container CObList

给你一囗甜甜゛ 提交于 2020-01-14 03:19:10
问题 I have a Folder class which contains two lists of Folders and Files . class Folder : public CObject { public: typedef std::string StringT; ... Container filesInFolder; Container foldersInFolder; StringT folderName; ... }; class File : public CObject { ... }; Lists derive from CObList. Class Container : public CObList . I need to perform search inside foldersInFolder by name and in filesInFolder by name and extension. CObList::Find does not allow me to use predicates. I would normaly use std:

non-member function pointer as a callback in API to member function

假装没事ソ 提交于 2020-01-13 14:04:13
问题 I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class in C++ but I'm getting compilation errors. The API definition is: typedef void (__stdcall *STREAM_CALLBACK)(void *userdata); __declspec(dllimport) int __stdcall set_stream_callback( STREAM_CALLBACK streamCB, void *userdata); One example file, provided by the third party, is: void __stdcall streamCB(void *userdata) { // callback implementation } int main(int argc, const char

What is a function type used for?

一笑奈何 提交于 2020-01-13 07:49:13
问题 Given the following two typedef s: typedef void (*pftype)(int); typedef void ftype(int); I understand that the first defines pftype as a pointer to a function that takes one int parameter and returns nothing, and the second defines ftype as a function type that takes one int parameter and returns nothing. I do not, however, understand what the second might be used for. I can create a function that matches these types: void thefunc(int arg) { cout << "called with " << arg << endl; } and then I

How do you pass a function of a class as a parameter to another function of the same class

萝らか妹 提交于 2020-01-13 06:47:47
问题 i basically want to use a dif function to extract a different element of a class (ac). the code is similar to this: .h: class MyClass { public: double f1(AnotherClass &); void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)); }; .cc: double MyClass::f1(AnotherClass & ac) { return ac.value; } void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)) { std::cout << f1(ac); } didn't work, it gives error#547 "nonstandard form for taking the address of a member

the arrow '->' separator is crashing when calling function from class

核能气质少年 提交于 2020-01-11 12:58:15
问题 I'm working on a project for class and I'm using classes and pointers of type class to call some functions in the class but it's crashing on Code Blocks and Eclipse and I don't know what is going on Note it crashes when assigning x with y #include <iostream> using namespace std; class a{ private: int x; public: void set_X(int y){ x=y; } }; int main() { a *Ptr; Ptr->set_X(5); } 回答1: a *Ptr; Ptr->set_X(5); Your Ptr does not point to anything. Trying to invoke a member function on an

How to call a function using pointer-to-member-function

感情迁移 提交于 2020-01-11 11:25:07
问题 I have a class: class A { void test_func_0(int); void run(); typedef void(A::*test_func_t)(int); struct test_case_t{ test_func_t test_func; } test_case[100]; }; Now I want to call test_func() inside run(): void A::run() { test_case[0].test_func = &test_func_0; test_case[0].*(test_func)(1); } The last line of my code, doesn't work(compile error), no matter what combination I try. 回答1: Use this: void A::run() { test_case[0].test_func = &A::test_func_0; (this->*(test_case[0].test_func))(1); }

Incrementing function pointers

你说的曾经没有我的故事 提交于 2020-01-11 08:12:37
问题 I just learned about function pointers (pointers pointing at the adress where where the machine code of a function is stored). This made me think about machine code and how it is stored in memory. Is the machine code stored consecutively in memory, so that it is possible to "manually" increase the pointer until it points to the following/previous function? Is this, what a debugger does? He lets me "see" where the program counter is pointing in the machine code? Conclusion: one can program

Incrementing function pointers

天涯浪子 提交于 2020-01-11 08:12:25
问题 I just learned about function pointers (pointers pointing at the adress where where the machine code of a function is stored). This made me think about machine code and how it is stored in memory. Is the machine code stored consecutively in memory, so that it is possible to "manually" increase the pointer until it points to the following/previous function? Is this, what a debugger does? He lets me "see" where the program counter is pointing in the machine code? Conclusion: one can program

Clean implementation of function template taking function pointer

喜欢而已 提交于 2020-01-11 06:52:12
问题 I've managed to implement and test my function wrapper implementation, however, the interface isn't as nice as it should be: template < typename F, F* f > void register_function( const char* name ) { int (*lf) (lua_State *) = function_wrapper< F, f >; register_native_function( lf, name ); } While this works as expected, the usage requires to explicit template parameters: register_function< decltype(hello), &hello >( "hello" ); Obviously the first parameter could be deduced from the first one,