What is
sortSortablesMethod
?
There's two simple solutions:
1. Make the sort predicate a free function.
2. Make the sort predicate a static member function.
3. Use std::bind() or std::function() to create a closure over the class context.
4. Just put the sorting predicate in a lambda, inline. Judging by the sorting code, this would work:
1 2 3 4
|
std::sort(newVector.begin(), newVector.end(),
[] (Sortable *a, Sortable *b) {
return a->value() > b->value();
});
| |
Some other potential issues:
The signature of SpreadSheet::sortMethod implies that it modifies the Sortable objects it accesses. That's not allowed, so perhaps change the signature to accept
Sortable const *const
instead. You might have to mark
Sortable::value()
const
as well.
Why are you storing pointers in a vector? That puts the burden of memory access on you. Store the values by default; if that's not acceptable, consider
std::reference_wrapper
or preferably a smart pointer that reflects the ownership semantics you want. (Probably you want
std::unique_ptr
, if this class
Sortable
is polymorphic?)
Note: (this is why the sort() invocation doesn't compile)
Function pointers are one thing.
Non-static member function pointers require context to invoke - their environment - an associated instance of the class.
This makes sense, because in order to call a member function at any point, you have to have a class instance to operate upon:
my_instance.do_stuff(args);
When you're trying to call a member function through a member-function pointer, you need a class instance on the left, and special syntax in-between. Here's an example.
1 2 3 4
|
/* The following line creates a member-function pointer to SpreadSheet::sortMethod. */
/* But which SpreadSheet specifically? We don't know yet. */
bool (&SpreadSheet::*sort_fn)(Sortable*, Sortable*) = &SpreadSheet::sortMethod;
my_spreadsheet_instance.*sort_fn(a, b);
| |
std::sort assumes that it recieves some sort of normal function or functor (i.e., not a member function) but since it's a template you'll get a compile error inside
std::sort()
as the result of
complete substitution failure.