 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Wed Jul 05, 2006 8:10 am Post subject: How to use a list's sort() with my own sort-function, if thi |
|
|
How to use a list's sort() with my own sort-function, if this one is
inside a class?
Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.
When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.
Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as
A value of type void is not valid
Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.
Is there a solution?
Thanks,
Michael |
|
| Back to top |
|
 |
Thorsten Kettner Guest
|
Posted: Wed Jul 05, 2006 8:10 am Post subject: Re: How to use a list's sort() with my own sort-function, if |
|
|
Please see my response in vcl.components.using. |
|
| Back to top |
|
 |
Sabetay Toros Guest
|
Posted: Wed Jul 05, 2006 6:41 pm Post subject: Re: How to use a list's sort() with my own sort-function, if |
|
|
MR wrote:
| Quote: | How to use a list's sort() with my own sort-function, if this one is
inside a class?
Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.
When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.
Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as
A value of type void is not valid
Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.
Is there a solution?
Thanks,
Michael
Michael, |
Use boost library;
An example
class TFoo
{
public :
int my_sort(const struct my_type &s1, const struct my_type &s2);
};
FList is populated with TFoo objects;
sort(FList.begin(), FList.end(), boost::bind(&TFoo::mysort,
this, _1, _2) ); |
|
| Back to top |
|
 |
Sabetay Toros Guest
|
Posted: Wed Jul 05, 2006 6:44 pm Post subject: Re: How to use a list's sort() with my own sort-function, if |
|
|
MR wrote:
| Quote: | How to use a list's sort() with my own sort-function, if this one is
inside a class?
Inside a class of mine, I want to use STL's sort() function for a list
with my own sort-routine.
When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.
Making it a methode of my class returns a compile-error inside
(Dinkum's) list.hpp, #942, translated as
A value of type void is not valid
Unfortunately inside my function I need access to other (private)
members of my class, so it really should be realized as part of the
class.
Is there a solution?
Thanks,
Michael
Michael, |
Use boost library;
An example
class TFoo
{
public :
list< my_type > FList;
int my_sort(const struct my_type &s1, const struct my_type &s2);
};
sort(FList.begin(), FList.end(), boost::bind(&TFoo::mysort,
this, _1, _2) );
Sabetay |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jul 08, 2006 7:16 pm Post subject: Re: How to use a list's sort() with my own sort-function, if |
|
|
| Quote: | Use boost library;
|
Perhaps I should really think about installing boost...
Thank you,
Michael |
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Wed Jul 12, 2006 8:10 am Post subject: Re: How to use a list's sort() with my own sort-function, if |
|
|
<MR> wrote:
| Quote: | How to use a list's sort() with my own sort-function, if this
one is inside a class?
When defining it as
int my_sort(struct my_type &s1, struct my_type &s2) {
...
}
outside(!) my class this compiles fine.
(Dinkum's) list.hpp, #942, translated as
A value of type void is not valid
Unfortunately inside my function I need access to other
(private) members of my class, so it really should be
realized as part of the class.
|
Declare the function as static. Then pass &ClassName::my_sort
to the sort function. (There's no way to pass a non-static
member function to the sort function, so I am curious as to
what you wrote to get that Dinkum error message :)
Also it would be good to call it "my_compare" or something
similar, as the purpose of the function is a comparison
(not a sort). It may turn out that the function is useful
for comparisons in other situations.
Make sure that your function is a strict ordering,
otherwise the sort may malfunction:
1) X < Y is always the same (if X and Y are the same)
2) if X < Y and Y < Z, then X < Z
3) if X < Y is true then Y < X is false, and vice versa
4) if X == Y, then X < Y and Y > X are both false.
Usually a good design involves objects being sortable
based on publicly accessible data alone. It's best practice
to have the compare function a non-member, so you may want
to rethink your design a little. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|