I have a project with a single .h
file and multiple .cpp
files. The header file contains a namespace UF
(abbreviation for useful functions) that currently implements sorting.
This is done by having a comparator defined in UF.cpp
thus:
int compar_int_asc(const void *a, const void *b)
{
int aa = *((int *)a), bb = *((int *)b);
if (base_arr_int[aa] < base_arr_int[bb])
return -1;
if (base_arr_int[aa] == base_arr_int[bb])
return 0;
if (base_arr_int[aa] > base_arr_int[bb])
return 1;
}
At present, the base array base_arr_int
that needs to be accessed by qsort
and the comparator function above is declared in main.cpp
and externed in UF.cpp
.
I access qsort
within a different class, SEP
as follows. Firstly, in SEP.cpp
, I extern base_arr_int
. Then, if ratios[100]
is an integer array that is native and local to SEP
, I do the following within SEP.cpp
.
base_arr_int = ratios;
qsort(indices, 100, sizeof(int), UF::compar_int_asc);
Is this the best way of implementing qsort with multiple classes?
In particular, I would like to avoid using global variables defined in main.cpp
as much as possible. Is there any alternative design?