Page 1 of 1

c++ callback function in class

Posted: Mon Sep 16, 2019 12:37 pm
by asdasd
Hi all
I am using chibios with c++ compiling but I have some problem in function callback and here is my code:

Code: Select all

class firstclass{
firstclass (palcallback_t cb);
}

class secandclass{
secandclass();
void butt_cb(void *arg);
firstclass fun(butt_cb);
}


I don't have error if i put the function static ,but i don't want to do that .
when I have tried to write

Code: Select all

firstclass fun(std::bind(&secandclass::butt_cb));

i failed

asd.

Re: c++ callback function in class

Posted: Mon Sep 16, 2019 12:46 pm
by Giovanni
Callbacks must be C functions not C++ methods.

Giovanni

Re: c++ callback function in class

Posted: Mon Sep 16, 2019 1:04 pm
by asdasd
Giovanni wrote:Callbacks must be C functions not C++ methods.

Giovanni


is there any way to call in c++

Re: c++ callback function in class

Posted: Mon Sep 16, 2019 8:58 pm
by steved
Have a 1-line C function which calls the C++ function. The parameter to the callback is the object pointer.

Re: c++ callback function in class

Posted: Tue Sep 17, 2019 2:32 pm
by alex31
° you can also pass the address of a static member function of the class.

° another convenient way is to use captureless lambda, but you need a c++11 compliant compiler.

Alexandre