I'm trying to get a function to accept several types of data (int float, char, etc) and preform the same function on them.
I'm overloading the function by using functions that accept different types and call the main function with the type.
My main function then asignes a pointer to the pointer it has just been given and works with the data.Code:float mmmm(void* array, int size, int type); float avg(int* array, int size) {return mmmm(array, size, 1);} float avg(long* array, int size) {return mmmm(array, size, 2);} float avg(float* array, int size) {return mmmm(array, size, 3);} //etc.
Then to work with the data I use indirrection twiceCode:float mmmm(void* array, int size, int type) { void* ptr = NULL; switch (type) { case 1: ptr = new int*; *ptr = (int*)array; break; case 2: ptr = new long*; *ptr = (long*)array; break; case 3: ptr = new float*; *ptr = (float*)array; break; //etc.
This fragment adds the sum of the data in the array
Why won't the compiler compile this?Code:long double sum; for(int i = 0; i < size; i++) sum += *(*ptr + i);
How can I get a function to accept several types of data without writing out many copies of it?



LinkBack URL
About LinkBacks


