dimanche 16 avril 2023

Is there a way to automatically generate overriding function for child classes?

I have a snippet of code in my system, put it short, there are three classes, A, B extends A, and a singleton service C. C implemented one function, and I want it's behavior customizable based on caller.

class A {

void abort() {
  C::doSomething();
}

}

class B : public A {}

class C {

void doSomething() {
  ... a bunch of logics here.
}

}

There are multiple child classes extending A, and the current implementation is they enumrate all cases in doSomething, but I realized the cases are actually related to the caller. So I want to make the code looks like:

class A {

virtual void abort() = 0;

}

class B : public A {

void abort() override {
  C::doSomething<B>();
}

}

class C {

template <class T>
void dosomething() {
  ...T specific logic.
}

}

And since the overrided abort function only do one thing that is related to the implementing class, I wonder if there is a mechanism to make this automatic that I don't need to make every child class implement it (because I need to teach other people more about how to integrate into the system, and I prefer it works with least amount of code added.)

Thanks.

Aucun commentaire:

Enregistrer un commentaire