Small background: I have a C code library that is part of a larger system (all C). For this part of the C library, you must create a graphical interface that allows users to play with parameters. For the graphical interface, I chose QT, as cross-platform support is required. I use Eclipse and MinGW as an IDE and compiler (but I think the question is more language than specific to the compiler?).
With QT, I created a widget that contains a pointer to a structure implemented in C that contains pointers to several functions that execute the logic of the C library.
extern "C" {
#include "c-src/CLogic.h"
}
QTWidget::QTWidget(QWidget *parent)
{
this->rtw.displayText = &QTWidget::displayText;
this->clogic = CLogic_getInstance(&rtw);
}
void QTWidget::buttonClicked()
{
this->clogic->buttonClicked();
}
void QTWidget::displayText(char *text, int position)
{
ui.textItem->setText(text);
}
GUI, QTWidget:: buttonClicked(), C - . : CLogic QTWidget RefToWidget, .
static CLogic instance;
void CLogic_buttonClicked()
{
instance.rtw->displayText("Hello World", 1);
}
CLogic* CLogic_getInstance(RefToWidget *rtw)
{
instance.rtw = rtw;
instance.buttonClicked = &CLogic_buttonClicked();
}
, ( , QT slotClicked(), CLogic_buttonClicked(), QTWidget:: displayText (), , . char * 0x1 , , int ().
C CPP?
EDIT @Luccas Matteis:
#ifdef __cplusplus
#include "QTWidget.h"
extern "C" {
#endif
struct RefToWidget{
#ifdef __cplusplus
void (QTWidget::*displayLine)(char* text, int lineNumber);
#else
void (*displayLine)(char* text, int lineNumber);
#endif
};
typedef struct RefToWidget RefToWidget;
#ifdef __cplusplus
}
#endif
, , , "" ( , , ... ...;))