By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,420 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,420 IT Pros & Developers. It's quick & easy.

Passing different sort methods to multiset

P: n/a
I'm trying to use an object in two different multiset containers, each
with it's own sort method. For the most frequently used, I overrode
the operator< method in the class, and for the second I wrote a method
LocationSort(). I can't seem to arrive upon the correct syntax for
defining a multiset that will use LocationSort.

multiset<DotObject,DotObject::LocationSort> mySet;

fails.

Is there a correct way to do this while keeping LocationSort inside of
my class? I know I could wrap it in a separate structure and pass in
the structure name, but would prefer to keep it inside the class code
for ease-of-maintenance reasons.

Thanks.

Dale

Jan 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Dale Marchand" <da***********@gmail.com> schrieb im Newsbeitrag
news:11*********************@g43g2000cwa.googlegro ups.com...
I'm trying to use an object in two different multiset containers, each
with it's own sort method. For the most frequently used, I overrode
the operator< method in the class, and for the second I wrote a method
LocationSort(). I can't seem to arrive upon the correct syntax for
defining a multiset that will use LocationSort.

multiset<DotObject,DotObject::LocationSort> mySet;

fails.

Is there a correct way to do this while keeping LocationSort inside of
my class? I know I could wrap it in a separate structure and pass in
the structure name, but would prefer to keep it inside the class code
for ease-of-maintenance reasons.


Try to make LocationSort a static member function (with two parameters) of
DotObject. If that doesn't work (I didn't try it), you can still make a
separate struct/class. Such a struct can be defined inside the DotObject
class, like

class DotObject
{
public:
struct LocationSort
{
bool operator()(DotObject const& lhs, DotObject const& rhs)
const
{
return ...
}
};
...
};

HTH
Heinz
Jan 3 '06 #2

P: n/a
Thanks. I just hit upon option 2 about 10 seconds prior to reading
your post (no, really I did!). It works fine....I'd just think there'd
be a simpler, more elegent way.

I'd prefer to stay away from static members, just because I've got a
bunch of threads running and prefer to stay away from the synch hassles
that static's bring.

Thanks.

Dale

Jan 3 '06 #3

P: n/a
On 3 Jan 2006 13:11:24 -0800 in comp.lang.c++, "Dale Marchand"
<da***********@gmail.com> wrote,
I'd just think there'd be a simpler, more elegent way.
I'd prefer to stay away from static members
The compare function is called with two arguments, and not as a
member function. If you want to use a member function, you can wrap
it in a std::mem_fun() adapter. See Stroustrup chap. 18.4.4.2
just because I've got a
bunch of threads running and prefer to stay away from the synch hassles
that static's bring.


No reason a static member _function_ (without any local static
variables etc.) should cause a problem.

Jan 3 '06 #4

P: n/a
I read your reference on mem_fun, and it looks like I'm limited to 1
arg member functions at most. Since the Compare function requires 2
args, I don't see how I can use that, unless I'm missing something?

Jan 4 '06 #5

P: n/a
On 4 Jan 2006 10:31:43 -0800 in comp.lang.c++, "Dale Marchand"
<da***********@gmail.com> wrote,
I read your reference on mem_fun, and it looks like I'm limited to 1
arg member functions at most. Since the Compare function requires 2
args, I don't see how I can use that, unless I'm missing something?


What you are missing is that in a member function, the object upon
which the function is invoked is equivalent to another argument.
Foo.memfun(Bar) and nonmemfun(Foo, Bar) have effectively the same
number of arguments.

Jan 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.