Pointer to C++ function
-
wrote on 4 Oct 2014, 20:46 last edited by
Is there a way to pass to a C function a pointer to C++ function?
This is the C function
@int wiringPiISR (int pin, int edgeType, void (*function)(void)) ;@My C++ functiin is this:
@QThlpt::OnData;@I can create a pointer to C++ function but the pointer is not valid for a C function.
@ funptr ptr;
ptr= &QThlpt::OnData;
@ -
wrote on 4 Oct 2014, 21:34 last edited by
Hi, if you make OnData() static you can do it.
-
wrote on 4 Oct 2014, 21:49 last edited by
I know but if I declare as static the function I can't use non static objects as qvector or QDateTime, declared in the class...
-
wrote on 4 Oct 2014, 22:40 last edited by
You could create static copies of your qvector and QDateTime objects as well.
-
wrote on 5 Oct 2014, 07:32 last edited by
The object has an own QVector, I have to append data to it. Is there a way?
-
Hi,
No you cannot pass a pointer-to-non-static-member function, because its type is different. See http://www.parashift.com/c++-faq/fnptr-vs-memfnptr-types.html
- Your C function accepts pointers of type void (*)(void), but
- Your member function's type is void (QThlpt::*)(void)
If your compiler supports C++11, you can use std::bind() or a lambda to create a function of type void (*)(void) that modifies your C++ object. Then, you can pass this function to wiringPiISR()
-
wrote on 5 Oct 2014, 15:46 last edited by
Solved modifing the exteran c code by passing to it a pointer to my object and a pojnter to my function.
-
wrote on 23 Oct 2014, 14:07 last edited by
[quote author="JKSH" date="1412506264"]
If your compiler supports C++11, you can use std::bind() or a lambda to create a function of type void (*)(void) that modifies your C++ object. Then, you can pass this function to wiringPiISR()[/quote]Could you give us an example of implementation?
-
wrote on 23 Apr 2018, 13:11 last edited by Monni
In case of someone else wanting to have wiringPiISR() function to work in a class, I wrestled a couple of days with this issue myself. I managed to create a dirty workaround (I know, ew, global pointer) for this, and want to share it to someone who looks for the same thing:
Create static method with your normal method within MyISRClass.h:
public: void myFunction(); static void isrCatcher();
In main.cpp, create global pointer to remember the instance the ISR initialization is in:
MyISRClass *isrClass = 0;
Store the address of the class instance:
extern MyISRClass *isrClass MyISRClass::MyISRClass() { ... isrClass = this; wiringPiISR(pin, INT_EDGE_BOTH, isrCatcher); ... } void MyISRClass ::isrCatcher() { isrClass->myFunction(); }
I hope this helps someone who comes to look for the same answer. I'm a noob with c++, so this probably isn't the safest or even secure way to do this, but it works if you just have to get that Raspberry Pi program to work with simple knowledge.
And don't forget to include LIBS += -L/usr/local/include -lwiringPi -line to your .pro file when compiling.
-
wrote on 23 Apr 2018, 14:10 last edited by
Common pattern for using C callbacks with C++ classes is passing static method (or internal static free function) as a callback pointer, and using
this
as a "data" pointer which is usually passed into callback asvoid*
argument when it's called (in your callback code you cast it into pointer to your class and proceed)static ReturnType A::callbackMethod(<arguments>, void *data) { A* self = static_cast<A*>(data); // Following code uses self instead of this // ... }
-
wrote on 23 Apr 2018, 14:55 last edited by
Many thanks for your post but this thread is very outdated.
Regards.