function-pointers

Calling a function with internal linkage via pointer from another translation unit

人走茶凉 提交于 2019-12-10 19:58:01
问题 Can we declare a callback function in an anonymous namespace (thus giving it internal linkage), knowing that it will be called from another translation unit (another library even)? Some lib: void register_callback(void (*cb)()) { .. cb(); .. } Main program namespace { int foo_cb() { ... } // internal linkage } int main() { register_callback(foo_cb); } 回答1: TL;DR: yes, it is ok From [basic.link] (emphasis mine): A name is said to have linkage when it might denote the same object, reference,

Passing static method as argument, no address-of operator required?

 ̄綄美尐妖づ 提交于 2019-12-10 19:07:13
问题 class ThreadWorker { public: ThreadWorker(void); virtual ~ThreadWorker(void); static void DoSomething(); }; int main() { boost::thread thread1(ThreadWorker::DoSomething); boost::thread thread2(ThreadWorker::DoSomething); boost::thread thread3(&ThreadWorker::DoSomething); } I'm playing around with Boost.Thread and I notice it doesn't seem to matter whether I use the address of operator (&) or not when passing a static member function as an argument. Does it not matter? And if not, why? Is one

lvalue required as left operand of assignment for function pointers

纵饮孤独 提交于 2019-12-10 18:17:32
问题 I am trying to assign a function to a function pointer, but I am getting the following error: lvalue required as left operand of assignment. My code is the following: #include <stdio.h> void intr_handler(int param){ printf("Hey there!\n"); } int main(){ void *(intr_handlerptr)(int); intr_handlerptr = intr_handler; } I cannot see what is the problem there, since I am assigning to the pointer "intr_handlerptr" the function "intr_handler" and they have the same signature. What am I missing? 回答1:

Having class or function as template parameter

百般思念 提交于 2019-12-10 18:06:03
问题 I wasn't sure what to search for, so I'll try to explain as best as possible. In the STL, std::set is defined as template <class Key, class Compare, class Allocator> class set; From http://cplusplus.com: Compare : Comparison class: A class that takes two arguments of the same type as the container elements and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are elements of the container [...] . This can either be a class implementing a

How do I declare a function that returns a function pointer?

烈酒焚心 提交于 2019-12-10 17:56:50
问题 Imagine a function myFunctionA with the parameter double and int: myFunctionA (double, int); This function should return a function pointer: char (*myPointer)(); How do I declare this function in C? 回答1: void (*fun(double, int))(); According to the right-left-rule, fun is a function of double, int returning a pointer to a function with uncertain parameters returning void . EDIT: This is another link to that rule. EDIT 2: This version is only for the sake of compactness and for showing that it

C++ External function with pointer to function as parameter, used inside a class with a member function

时光总嘲笑我的痴心妄想 提交于 2019-12-10 17:42:13
问题 Fairly new to C++. Suppose I have a class: class A { private: double m_x, m_y; public: A(double x, double y): m_x {x} { m_y = extF(m_x, y, *intF); } double intF(double x) { return 2*x; } }; And it makes use of an external global function, defined elsewhere: double extF(double x, double y, std::function<double(double)> f) { if (x*y < 0) return f(x); else return f(y); } Formulas are bogus. This does not compile. I tried simple intF , A::*intF , &A::intF , even some unorthodox combinations, but

Avoiding a static member function in c++ when using a callback interface from C

守給你的承諾、 提交于 2019-12-10 17:28:33
问题 I would like to access the data within this member function that is static. Right now the member function is static so that I can use it with a third party API written in C that has typdef function pointer for callback purposes. Based on the info below, what is the best way to get around the need to create a static function in order to use the data from the following function member within other member functions of my class that are non-static. Maybe there is a way to still use this static

How to use pointer to member function when when pointer to global function is required?

∥☆過路亽.° 提交于 2019-12-10 16:46:27
问题 I have the following problem. I have to use a function that takes a callback. Implementing the callback is the tricky part, because I need more information beyond that I can extract from the input parameters. I will try to give an example: typedef int (*fptr) (char* in, char* out); // the callback i have to implement int takeFptr(fptr f, char* someOtherParameters); // the method i have to use The problem is that I need additional info except the "in" parameter to construct the "out" parameter

What’s the best way to cast a function pointer from one type to another?

久未见 提交于 2019-12-10 16:29:29
问题 I’ve searched Stack Overflow for an answer, but I get nothing specific to this problem: only general cases about use of various types of cast operators. So, the case in point is when retrieving a function address with the Windows GetProcAddress() API call, which returns a function pointer of type FARPROC , with: typedef INT_PTR (__stdcall *FARPROC)(); . The trouble is, the actual function sought rarely (if ever) has this actual signature, as shown in the MRCE code, below. In this code, I have

Is it safe to pass function pointers as arguments to dll functions and invoke them from inside of the dll?

元气小坏坏 提交于 2019-12-10 15:15:42
问题 I would like to pass some (dll or not) function pointers as arguments to some dll functions and call them from inside of the dll. I wonder if it is safe because I have found an information on http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.cbcpx01/fpref.htm that: In DLL code, it is assumed that a function pointer points to a function descriptor. A function pointer call is made by first obtaining the function address through dereferencing the pointer; and