我试图将Piconomic HDLC C模块转换为C ++类。
http://www.piconomic.co.za/fwlib/hdlc_8h_source.html http://www.piconomic.co.za/fwlib/hdlc_8c_source.html
我无法将函数指针从Qt程序 MainWindow类传递到HDLC类。 函数指针可以在HDLC hdlc_init函数中传递,也可以传递给采用这些参数的HDLC HDLC构造函数。
假设我们有MainWindow和HDLC -classes,如何更改这些以指向
MainWindow::putChar(char data) and
MainWindow::onRxFrame(const u8_t *buffer, u16_t bytes_received)
class HDLC {
/**
Definition for a pointer to a function that will be called to
send a character
*/
typedef void (*hdlc_put_char_t)(char data);
/**
Definition for a pointer to a function that will be called once a frame
has been received.
*/
typedef void (*hdlc_on_rx_frame_t)(const u8_t *buffer, u16_t bytes_received);
}
void hdlc_init(hdlc_put_char_t put_char,
hdlc_on_rx_frame_t on_rx_frame);
}
// HDLC.cpp:
/// Pointer to the function that will be called to send a character
static hdlc_put_char_t hdlc_put_char;
/// Pointer to the function that will be called to handle a received HDLC frame
static hdlc_on_rx_frame_t hdlc_on_rx_frame;
void HDLC::hdlc_init(hdlc_put_char_t put_char,
hdlc_on_rx_frame_t on_rx_frame)
{
hdlc_rx_frame_index = 0;
hdlc_rx_frame_fcs = HDLC_INITFCS;
hdlc_rx_char_esc = FALSE;
hdlc_put_char = put_char;
hdlc_on_rx_frame = on_rx_frame;
}
甚至可以这样做,HDLC被赋予一个指向MainWindow类函数的指针?
答案 0 :(得分:0)
您可以将您的功能定义为:
typedef std::function<void(char)> hdlc_put_char_t;
然后要传递MainWindow类函数指针,可以使用bind
,例如:
MainWindow *mw = new MainWindow;
hdlc.hdlc_init(std::bind(&MainWindow::someFun, mw), ...);
这是因为类成员函数指针需要一个在其上调用它们的对象。