M
Martin Herbert Dietze
Hello,
I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.
In half-code this should in the end look like this:
In the child class:
| int ChildClass::fooHandler(int a, char b)
| {
| //[...]
| }
|
| int ChildClass::barHandler(int a, char b)
| {
| //[...]
| }
|
|
| void ChildClass::init (void)
| {
| registerMethod ("foo", &fooHandler);
| registerMethod ("bar", &barHandler);
| }
In the parent class:
| int ParentClass::runHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);
| }
In the real life problem, the code in the parent class is of
course much more complex. In the existing implementation this
is all done in the child classes, so that we got a lot of
unnecessary redundance in the code.
The problem is that because different child classes would
define different numbers of such methods (and thus also of
different names) using abstract methods does not seem to help
here.
Has anybody managed to get something like this running?
Cheers,
Martin
I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.
In half-code this should in the end look like this:
In the child class:
| int ChildClass::fooHandler(int a, char b)
| {
| //[...]
| }
|
| int ChildClass::barHandler(int a, char b)
| {
| //[...]
| }
|
|
| void ChildClass::init (void)
| {
| registerMethod ("foo", &fooHandler);
| registerMethod ("bar", &barHandler);
| }
In the parent class:
| int ParentClass::runHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);
| }
In the real life problem, the code in the parent class is of
course much more complex. In the existing implementation this
is all done in the child classes, so that we got a lot of
unnecessary redundance in the code.
The problem is that because different child classes would
define different numbers of such methods (and thus also of
different names) using abstract methods does not seem to help
here.
Has anybody managed to get something like this running?
Cheers,
Martin