c++ callback function in class Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
asdasd
Posts: 18
Joined: Wed May 01, 2019 8:54 am
Has thanked: 9 times

c++ callback function in class

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: c++ callback function in class

Post by Giovanni »

Callbacks must be C functions not C++ methods.

Giovanni
asdasd
Posts: 18
Joined: Wed May 01, 2019 8:54 am
Has thanked: 9 times

Re: c++ callback function in class

Post by asdasd »

Giovanni wrote:Callbacks must be C functions not C++ methods.

Giovanni


is there any way to call in c++
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: c++ callback function in class

Post by steved »

Have a 1-line C function which calls the C++ function. The parameter to the callback is the object pointer.
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: c++ callback function in class

Post 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
Post Reply