The idea is to not call getValue(X) directly. Instead call it indirectly.
One way to do that is to create a pure virtual class (an "interface") and let your callback classes inherit from that:
struct GetValue {
virtual ~GetValue() {}
virtual double get(IloNumVar x) = 0;
};
struct Callback1 : public IloCplex::LazyConstraintCallbackI, GetValue {
double get(IloNumVar x) { return getValue(x); }
};
struct Callback2 : public IloCplex::UserCutCallbackI, GetValue {
double get(IloNumVar x) { return getValue(x); }
};
Now you change your common code
void commonFunction(...) {
callback->getValue(x);
}
to something like this
void commonCode(..., GetValue *getValue) {
getValue->get(x);
}
That is, instead of calling callback->getValue() directly in your common code, you pass an instance of GetValue to your common code and get the value through that instance. From your callback's main function you can then just call commonCode(..., this).
I hope you get the idea?
Using templates and parametrized inheritance you can write even cleaner code. Something like this:
template<typename T>
struct CallbackBase : public T {
void commonCode(...) {
getValue(x);
}
};
class Callback1 : public CallbackBase<IloCplex::LazyConstraintCallbackI> {
void main() { commonCode(...); }
};
class Callback2 : public CallbackBase<IloCplex::UserCutCallbackI> {
void main() { commonCode(...); }
};
Here the idea is to put the common code into a common base class of your callback. Using parameterized inheritance this common base class can either inherit from the lazy constraint callback or the user cut callback (or any other CPLEX callback that provides a getValue() function). Your actual callback functions then inherit from the common base class and can directly invoke the commonCode() function without having to pass additional arguments.
If you are sufficiently familiar with templates I would use the second variant as that leads to less obscure source code. The first approach is something that would work in Java as well.
#CPLEXOptimizers#DecisionOptimization