class A {
void methodToCall() {};
}
class B {
B() {
subscribeToEvent();
}
void eventHandler(Event* E) {
call A::methodToCall()
}
}
class C : public A, public B{
}
This may seem like a rare situation so let me explain what I’m trying to do.
I have alot of classes that are like class A. These are generated by another framework so I do not have control over how they are generated.
For every class A I have one or more class C‘s that inherit a class like A.
I have developed the need to call A::MethodToCall() when eventHandler() is called. My goal is not to have to subscribe and provide an eventhadler method in all of my classes like class C.
I would much rather create a one class B that all my class C’s can simply inherit.
My problem is I do not know how to make the call to class A from class B.
If there is a better way of doing this I’m open to it.
Your code doesn’t match your description (in the code B does not inherit from A, whereas in your description it does), but I think what you’re saying is “I have a large number of classes like A, and a large number of classes that inherit from classes like A (C in your example), and there’s a bunch of common stuff in those subclasses I’d like to factor out (into B in your example), but it includes stuff that depends on the specific A (calling an A method in your example) and it defeats the whole point to have to make many B’s (one for each A) mostly all alike.”
One thing you could use is the ‘template class inheriting from argument’ trick to define large numbers of B variants as a single template class:
If you have certain A classes that need slightly different stuff in B, you can also specialize the B template for those classes.