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, utzig, tfAteba, barthess

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

c++ callback function in class

Postby asdasd » Mon Sep 16, 2019 12:37 pm

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: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: c++ callback function in class

Postby Giovanni » Mon Sep 16, 2019 12:46 pm

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

Postby asdasd » Mon Sep 16, 2019 1:04 pm

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

Giovanni


is there any way to call in c++

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: c++ callback function in class

Postby steved » Mon Sep 16, 2019 8:58 pm

Have a 1-line C function which calls the C++ function. The parameter to the callback is the object pointer.

User avatar
alex31
Posts: 379
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 38 times
Been thanked: 62 times
Contact:

Re: c++ callback function in class  Topic is solved

Postby alex31 » Tue Sep 17, 2019 2:32 pm

° 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


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 49 guests