I'm trying to send a pointer to a class function as a variable to another class. I'm trying to keep both classes separate so I can reuse the code without having to edit it in the future.
I'm compiling on Windows 7, using Visual Studio 2015.
DATASTRUCT dataItem = {0, 1, 0, 1, dataText, parentClass::doSomething};
childClass.addItem(dataItem); //childClass is initiated as a member of parentClass
Elsewhere:
typedef struct dataStruct{
double min_x;
double max_x;
double min_y;
double max_y;
GLTEXT label; //custom struct for text information
void(*function)(void);
}DATASTRUCT;
I want childClass to be able to go about its business and check for certain parameters, and when childClass is done I want it to call doSomething from parentClass, without actually knowing it's a nested class.
The error I get during compile is:
void(parentClass::)() cannot be used to initialise an entity of void()().
I am able to define a standard function and call that, but that's not unique to each parentClass, and I then have to specify which parentClass::doSomething to which I am referring.
The simplest solution I have found is to redefine the pointer to function in childClass to void(parentClass::*)(), which is exactly what I want, but not how I want it.
With that said, I don't know where to take this, is there another way? Something similar?
Edit:
If possible, how would I explicitly send the information required for my childClass to properly interpret the function pointer dynamically.
So...
childClass.addItem(classInformation, dataItem, &parentClass::doSomething);
Or does the receiving end always have to know "parentClass::" in advance? Would this then be a good time to use void pointers?
Am I chasing after my own tail or is this going somewhere?
Edit:
Something like this seems dangerous, but, here it is.
childClass.addItem(classInformation, dataItem, static_cast<void*>(&parentClass::doSomething));
childClass::callParent(classInformation, void*function){
static_cast<classInformation.something*>(function)();
}
What would "classInformation.something" be in this case?