P
Peter Olcott
Does anyone know how to do this?
Does anyone know how to do this?
What I mean is as opposed to a global function or a static member function.Andre Kostur said:A member function of what? Your description is somewhat vague. A member
function of some other unrelated class. For example:
std::sort(somevector.begin(), somevector.end(), &otherclass::memfun);
? (and yes, I know the above won't work... but serves for illustration
purposes)
What I mean is as opposed to a global function or a static member
function. std::sort(RandomAccessIterator beg, RandomAccessIterator
end, BinaryPredicate op)
I want to somehow adapt a member function so that it can be
used as the BinaryPredicate parameter.
Andre Kostur said:Yes... a member of _what_ class? Some other unrelated class? Whatever
class *beg refers to?
any arbitrary class. I am only looking for the exact syntax
to convert a binary member function into a binary global function.
Something like mem_fun_ref
Andre Kostur said:Ummm.. you've got a problem though. Member functions are invoked on an
object. Where's std::sort going to get an instance of the object upon
which to invoke the mem_fun_ref?
std::sort is invoked from another member function.
Andre Kostur said:That's fine, but inside std::sort, how is it going to get a
pointer/reference to anything else (except of course the two iterators and
the BinaryPredicate)? When you call a global function (from within another
member function), how do you know from which object instance that global
function was called from?
class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
//
// The next line will not compile because LessThan is a member
function // I need the name of the Function Adaptor that converts
this into the // form required by std::sort. I don't know its name.
//
void sort(){ std::sort(Anything.begin(), Anything.end(), LessThan);
};
}
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
// return Any1 < Any2
// This MUST be a Member Function because it requires access
// to additional member data besides the std::vector data.
}
Andre Kostur said:OK, you'll need a helper class. See the post by Oliver... but correct it
to use pointers and references where appropriate (like the T a; member
variable should probably be T& a;, and the parameters to operator()
should be const-ref... that sort of thing), otherwise you'll probably end
up making unnecessary copies of stuff....
Oliver (Nospam) said:1. Approach (Member function has arbitrary name)
template <typename T>
struct helper: binary_function<T,T,bool>
{
T p;
helper(T p0): p(p0) {}
bool operator( T a, T b ) { return p->Member(a, b); }
}
T* myClass = new T;
sort(begin(),end(),helper(myClass));
2. Approach (Member function can be written as "bool operator(T a, T b)")
class T
{
bool member(T a, T b) {...};
bool operator(T a, T b) { return member(a,b); };
}
T* myClass = new T;
sort(begin(),end(),(*myClass ));
.... (removed, oliver)Peter Olcott said:"Oliver (Nospam)" <[email protected]> wrote in message
Oliver (Nospam) said:Yeah, I can help you. Let me first explain, my first approach just was
writing down the basic idea without any syntactical thoughts behind. Now
that I know, that's the thing you need, let us hav a look on further
details:
A compact version looks like this:
class Arbitrary {
std::vector<AnythingAtAll> Anything;
bool LessThan(const AnythingAtAll& Any1, const AnythingAtAll& Any2);
bool operator()(const AnythingAtAll& Any1, const AnythingAtAll& Any2) {
return LessThan(Any1, Any2); }
void sort(){ std::sort(Anything.begin(), Anything.end(),(*this)); };
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.