Hello Disch,
thank you for you reply.
I will study you ideas and see if I can do what I need with you informations.
But while reading once again my own message, I did not found it very clear about what I need so here are some more precisions.
In fact I need this for an event dispatcher.
I what to register for callback function pointers without having to define a register function for each type of Event (new events types can be created by inheriting the main Event class) and indeed I don't want to allow function with argument not being an Event or subclass of Event to be registered...
Consider the following example (with the previous class definition and testFunct_ definition) as a simpler and more direct example of what I try to make.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
Approach 1
void testFunc_Form( Shape obj ){}
void testFunc_Square( Square obj ){}
void testFunc_Dog( Dog obj ){}
void addFuncPtr( void(*FuncPtr)( Shape ) ) // Here is my problem (I want to allow only function with Shape argument or inheritors of Shape) !
{
// some code to register the Function pointer.
}
int main()
{
// The following is OK as it should.
addFuncPtr( testFunc_Form );
// The following should be OK (because Dog is not an inheritor of Shape) but in fact it is NOT (that is what I'm trying to change).
addFuncPtr( testFunc_Square );
// The following is NOT OK as it should (because Dog is not an inheritor of Shape).
addFuncPtr( testFunc_Dog);
return 0;
}
|
I have also tried with (void*) pointers, but as it now accept the Shape inheritors, it also accept non inheritors... and that's not very secure.
Here is the sample code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
Approach 2
void testFunc_Form( Shape obj ){}
void testFunc_Square( Square obj ){}
void testFunc_Dog( Dog obj ){}
void addFuncPtr( void* ) // Here is my problem (I want to allow only function with Shape argument or inheritors of Shape) !
{
// some code to register the Function pointer.
}
int main()
{
// The following is OK as it should.
addFuncPtr( (void*)testFunc_Form );
// The following is OK as it should (because Square is an inheritor of Shape).
addFuncPtr( (void*)testFunc_Square );
// The following is OK but I wish it's NOT (because Dog is not an inheritor of Shape).
addFuncPtr( (void*)testFunc_Dog);
return 0;
}
|
Approach 1, doesn't fit my need as the testFunct_Square (Square is inheritor of Shape) is not accepted.
Approach 2, works fine but it also doesn't really fit my need as it allow function using non inheritors of "Shape" as argument to be passed.
Hope that, those informations will be more clear than the previous one.
I will do some search using your suggestions.
Thank you
Edit : changed "Form" with "Shape" ... sorry.