You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.1 KiB

#include <stdint.h>
#include <unistd.h>
#include <iostream>
#include <functional>
class Device
{
public:
// cllback type
typedef std::function<unsigned int(unsigned int)> cb_t;
Device(cb_t cb) : cb(cb)//Device(cp_t cb) : cb(cb)
{
};
void do_something(void)
{
unsigned int return_value;
return_value = cb(1);
std::cout << return_value << std::endl;
};
private:
cb_t cb;
};
class Interface
{
public:
Interface()
{
std::cout << "created interface object" << std::endl;
};
unsigned int read_write(unsigned int input)
{
std:: cout << "calling read_write"<< std::endl;
std::cout << input << std::endl;
return 5;
};
private:
};
int main(void)
{
//using namespace std::placeholders; // for _1, _2, ...
std::cout << "callback_example" << std::endl;
Interface iface;
//unsigned int ret = iface.read_write(1);
//std::cout << ret << std::endl;
//auto cb = std::bind(&Interface::read_write, &iface,_1);
auto cb = std::bind(&Interface::read_write, &iface, std::placeholders::_1);
Device device(cb);
device.do_something();
return 0;
}