Is it idiomatically ok to put algorithm into class?
algorithm, c++, class
Solution
I have a complex algorithm. This uses many variables, calculates helper arrays at initialization and also calculates arrays along the way.[...]
Now, I actually do not see how this might be a class from an idiomatic way
It is not, but many people do the same thing you do (so did I a few times).
Instead of creating a class for your algorithm, consider transforming your inputs and outputs into classes/structures.
That is, instead of:
Calculation calc(a, b, c, d, e, f, g);
calc.calculate();
// use getters on calc from here on
you could write:
CalcInputs inputs(a, b, c, d, e, f, g);
CalcResult output = calculate(inputs); // calculate is now free function
// use getters on output from here on
This doesn't create any problems and performs the same (actually better) grouping of data.
Problem
I have a complex algorithm. This uses many variables, calculates helper arrays at initialization and also calculates arrays along the way. Since the algorithm is complex, I break it down into several functions. Now, I actually do not see how this might be a class from an idiomatic way; I mean, I am just used to have algorithms as functions. The usage would simply be: ``` Calculation calc(/* several parameters */); calc.calculate(); // get the heterogenous results via getters ``` On the other hand, putting this into a class has the following advantages: - I do not have to pass all the variables to the other functions/methods - arrays initialized at the beginning of the algorithm are accessible throughout the class in each function - my code is shorter and (imo) clearer A hybrid way would be to put the algorithm class into a source file and access it via a function that uses it. The user of the algorithm would not see the class. Does anyone have valuable thoughts that might help me out? Thank you very much in advance!