Hi All:
When I implement a member function of a class, I want to sort a vector using "sort" defined by stl in <algorithm>.
Every element of the vector to be sorted, is a struct:
1 2 3 4 5
|
struct Element
{
int index;
double value;
};
|
So I need to provide a sorting function "bool cmpVectorElement(Element first, Element second)" into "sort"
|
sort(my_vector.begin(), my_vector.end(), cmpVectorElement);
|
I want to define cmpVectorElement() as a protected member function of my class. But compiler gives me error.
I looked it up, and found that class member function pointer is actually different from traditional function pointers.
I know I can define the cmpVectorElement() function as static, as a work-around. But that's not a good solution in my case because I will have a derived class, and in that derived class, it will has its own version of cmpVectorElement(). Static would mean that my derived class will have to share the same cmpVectorElement() function of the base class.
So can anyone point out a solution? I need it to be in accord with good software engineering practice. Thanks!