function-pointers

C++: Store pointer to a member function of an object in another object

允我心安 提交于 2019-12-13 21:18:07
问题 I have a class which shall invoke a function specified by the user on certain occasions. Therefore the class has a method void setExternalPostPaintFunction(void(*function)(QPainter&)); that can be used to "register" a function. This function then will be called on that occasion: class A { public: void setExternalPostPaintFunction(void(*function)(QPainter&)); private: void (*_externalPostPaint)(QPainter&); bool _externalPostPaintFunctionAssigned; }; The function pointer is saved in the member

Printing time function to console produces 1 [duplicate]

梦想与她 提交于 2019-12-13 20:36:40
问题 This question already has answers here : How to print function pointers with cout? (7 answers) Closed last year . A couple of developers and I were wondering why: std::cout<<std::time<<std::endl; prints out a value of 1. What does the value represents, and why the value is of 1. Answer to what happened: How to print function pointers with cout? The C++ Standard specifies: 4.12 Boolean conversions 1 An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an

Strategy pattern or function pointer [closed]

五迷三道 提交于 2019-12-13 19:29:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . In C++ when I have algorithm which could accept different behaviour in runtime I rather use function pointer. For example, a program for drawing charts has one algorithm to draw line which can accept any function to specialize shape of this line. In Java there are no function pointers and I am

Writing a thunk to verify SysV ABI compliance

送分小仙女□ 提交于 2019-12-13 18:16:34
问题 The SysV ABI defines the C-level and assembly calling conventions for Linux. I would like to write a generic thunk that verifies that a function satisfied the ABI restrictions on callee preserved registers and (perhaps) tried to return a value. So given a target function like int foo(int, int) it's pretty easy3 to write such a thunk in assembly, something like 1 : foo_thunk: push rbp push rbx push r12 push r13 push r14 push r15 call foo cmp rbp, [rsp + 40] jne bad_rbp cmp rbx, [rsp + 32] jne

C++ How to create a function which takes as argument a reference to a function pointer?

与世无争的帅哥 提交于 2019-12-13 18:09:10
问题 i'm using function pointers typedef'd: typedef int(*ptr2Func)(); And a function which when given a number will select a function pointer for a function that is already declared. ptr2Func getPtr2Func(int function); So if u use getPtr2Func(1); u get a function pointer to the first function. However i want now to do the exact reverse thing: Pass a function pointer reference into a function which will in turn return the number of the function. i.e: i got 4 test functions which do nothing other

C++ calling static function pointer

霸气de小男生 提交于 2019-12-13 17:58:48
问题 I would like to forward a callback to a function pointer. So I declare a static (int*) m_pointer1, as well as a method void RegisterCallback1( (int*)fct) in class1.h: public: int RegisterCallback1( int (*fct) ); private: static int (*m_Callback1); in class1.cpp: int class1::RegisterCallback1( int (*fct) ) { m_Callback1= fct; } then, I want to forward the callback to the function pointer: void class1::Callback1() { (*m_Callback1)(); } But I get a compiler error "Expression must have (pointer

What is the purpose of Function Pointer syntax in C?

孤者浪人 提交于 2019-12-13 17:13:01
问题 EDIT: It's been pointed out that this question is sort of confusing. The short version is: "why have a separate pointer variable (eg, fnPtr ) which points to a function (eg, fn ) when the function name fn itself, without arguments, is already a pointer? /EDIT I'm trying to understand something and could use feedback from the community regarding function pointers. (And while this might look like a duplicate of other questions on the topic, it really isn't, as least not that I could find.) I

C function pointer type compatibility

雨燕双飞 提交于 2019-12-13 17:00:38
问题 Writing a library that works with function callbacks, I've frequently type-casted (and called) function pointers to types with the same calling convention and same signatures, but with one exception: they had parameters pointing to different types (all data), or void pointers. Recently, I discovered that it might not be that safe, according to this: https://stackoverflow.com/a/14044244/3079266 Basically, as I understood it, if the types of the arguments are compatible, that means the function

Assigning {pointer to function with C linkage} to {pointer to function with C++ linkage} and vice versa

二次信任 提交于 2019-12-13 16:24:41
问题 Is this code legal? extern "C" typedef void (ft_blah_c)(); /*extern "C++"*/ typedef void (ft_blah_cpp)(); extern "C" void fn_blah_c() {} /*extern "C++"*/ void fn_blah_cpp() {} ft_blah_c *g_Blah_c = fn_blah_cpp; // <--- ? ft_blah_cpp *g_Blah_cpp = fn_blah_c; // <--- ? I have real code with similiar assigments, it compiles and executes without any problems (MSVC 2010). 回答1: In general that should not work. The problem is that when you call fn_blah_c or fn_blah_cpp directly the compiler knows

Comparison of Virtual Function Pointers in C++

邮差的信 提交于 2019-12-13 13:26:45
问题 Say I want to check to see whether a subclass has implemented one of it's parent's virtual functions (never mind whether this smells of bad architecture... it's an exercise). If I wanted to see if two regular functions were identical, I could just check &f == &g . // Plain old functions void f() {} void g() {} ... std::cout << "&f " << &f << "\n"; // "1" OK, for some reason func ptrs are converted std::cout << "&g " << &f << "\n"; // "1" to booleans when printed. I can dig it. std::cout << "