How do i pass a pointer to a member function to std::list.sort()?
Is this possible? Thanks
struct Node { uint32_t ID; char * Value; }; class myClass { private: uint32_t myValueLength; public: list<queueNode *> MyQueue; bool compare(Node * first, Node * second); bool doStuff(); } bool myClass::compare(Node * first, Node * second) { unsigned int ii =0; while (ii < myValueLength) { if (first-> Value[ii] < second-> Value[ii]) { return true; } else if (first-> Value[ii] > second-> Value[ii]) { return false; } ++ii; } return false; } bool myClass::doStuff() { list.sort(compare); }
I want to use a length variable from within the class instead of doing strlen() within the compare function (The Value will always be the same length)
Edit: The myValueLength was not the only variable i wanted to access from within the comparison function I just simplified it to make the example shorter.
Elaborating on grieve’s response, why not use a functor? E.g.:
Then you could just use:
You could even use your class as the Functor by defining operator()…
Simple example code: