0
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.

3 Answers 3

1

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:

template<class A> B : public A { void eventHandler(Event* E) { call methodToCall(); } } class C1 : public B<A1> { ... whatever class C2 : public B<A2> { ... 

If you have certain A classes that need slightly different stuff in B, you can also specialize the B template for those classes.

Sign up to request clarification or add additional context in comments.

2 Comments

I would think this works (assuming that the underlying code is in C++ (i.e. able to pass on the details for A)
I think this is what I am after. I edited my original post. I meant to say class C when I said class B at one point but you picked up on that. Thanks!!!
0

This code

class A { void methodToCall() {}; } 

Should be

class A { static void methodToCall() {}; } 

For this to work:

 void eventHandler(Event* E) { A::methodToCall() } 

Because you do not have an object of type A and 'B' knows nothing of A - so only static methods will be available.

Alternatively do this

class B : public A .... class C: public B 

Then the static is not required as B is inheriting A.

The choice is yours.

3 Comments

I think I need to clarify. I have 100 classes like class C. And I have another 100 classes like class A. Each of the classes like class C inherit a corresponding class like class A. I want 1 class like class B that I can have all of my classes like class C inherit. The first solution will not work because I do not have the ability to edit classes like class A.
Why so many classes? I have worked on very large systems and that number of classes to handle events seems excessive. There should be a better solution that is more scalable, easier to document and debug.
I was trying to remain project agnostic.....But the class As are QT generated Ui_Files. The class Cs are wrappers where we implement our gui logic. I need to call retranslateUi on every class A when a particular event occurs. My goal is to create a class called Translatable (class B) that I can have all my class Cs inherit that require translation functionality.
0

You could write a wrapper class, which is a subclass of A and B. The subclass, call it D, would then know of both A and B, so when event occurs, D is responsible for handling it and calling A. Then you can have your class C just inherit class D.

EDIT: I think I misinterpreted your question here. If B inherits A, then B can call a function in A directly (as long as A's method is not private. If it were, you wouldn't be able to call it anyways).

2 Comments

I think I need to clarify. I have 100 classes like class C. And I have another 100 classes like class A. Each of the classes like class C inherit a corresponding class like class A. I want 1 class like class B that I can have all of my classes like class C inherit.
So effectively, you don't want to have to wire up each class C to inherit a class A, but rather to have all class C's inherit class B, which is smart enough to wire itself up to a corresponding class A based on your class C?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.