In general, there is a class LCgenerator , which uses the next() method to generate pseudo-random numbers. There is also a Sample class that should store values. I want to create a constructor of the Sample class that would call next() n times, thereby filling the selection.
Java programmers do this:
interface Generator { next(); } class LCgenerator implemets Generator { // определяю next(); } class Sample { public Sample(Generator g, int n) { // вызывать n раз функцию next() из экземпляра g класса, // реализующего интерфейс Generator } } Sample s = new Sample( new LCGenerator(param), n); How is such a problem solved in c ++? Interfaces in the language there. Do abstract class? It seems to me somehow ugly, or something. Use function pointers? Well then, it is not clear how to transfer the method of a specific instance to such a function.
template<class T> class Sample { //... Sample( T(*get)(), int n ); //... }; int main() { Sample<double> LC1( LCgenerator(1,7,3,32).next, 10000 ); //не компилит } Maybe somehow use a pointer to a method? In general, c ++ programmers, what solution (s) do you think is (are) beautiful (s)?