I tried to build a wrapper for a C module. The C module will do some CAN communication. To read something there is a read() function where a callback function pointer is passed with.
My idea was to make a Caller class where other classes (in example class A) can inherit from. I have now a problem in function "A::req()" with the comand "readObject()". To have no compiler errors I have to make a static_cast but then during excecution program will not work proberly. I already read that downcast in specialication can be dangerous. I guess my error is coming from that.
How could I solve my problem? Any ideas? Is there a design pattern which I should check?
Thanks.
class A : public Caller
{
public:
void req()
{
//readObject(&A::specializedCallback); // error from compiler
readObject(static_cast<Caller::CbkPtr_t>(&A::specializedCallback)); // has error during excecution, because of downcast?
}
void specializedCallback(void *data);
}
class Caller
{
public:
typedef void (CanIfcCaller::*CbkPtr_t)(void *data);
readObject(CbkPtr_t cbFcnt)
{
cbkFunction = cbFcnt;
Wrapper::readObject(this):
}
void callbackReadObject(void *data)
{
(this->*cbkFunction)(data);
}
private:
CbkPtr_t cbkFunction;
}
class Wrapper // to C function
{
public:
static void readObject(Caller* theCaller)
{
pCaller = theCaller;
read(&Wrapper::callbackReadObject);
}
static void callbackReadObject(void *data)
{
pCaller->callbackReadObject(data);
}
private:
Caller* pCaller;
}