C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can I (should I) transform this Function Object into a membe

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
darrin.wolf@gmail.com
Guest





PostPosted: Thu Oct 27, 2005 10:33 am    Post subject: Can I (should I) transform this Function Object into a membe Reply with quote



Greets to all of you C++ gurus,

(It's a fine profession that we share isn't it??) Love it!

....anyway, quick one (I think) for someone who knows more than myself
about member functions vs. function object predicates...

I wrote this class function:
//=== class function
//=== Think of cmdList (below) as a vector,
//=== where every record in the vector is another vector (of strings),
//=== with at least 4 string elements (referenced in
sortVectorClassContainerByWXYZ)
void BaseTaskMgmt::sortCommands(VectorClass &cmdList)
{
stable_sort(cmdList.begin(), cmdList.end(),
sortVectorClassContainerByWXYZ());
}

This class function (above) uses this "external" function object class:
//=== external (to BaseTaskMgmt) function object
class sortVectorClassContainerByWXYZ
{
public:
bool operator() (const VectorClassRow &left,
const VectorClassRow &right) const
{
long leftTpri = stol(left["task_pri"]);
long leftTseq = stol(left["virt_dual_seq"]);
long leftCpri = stol(left["pri"]);
long leftCseq = stol(left["virt_cmd_seq"]);
long rightTpri = stol(right["task_pri"]);
long rightTseq = stol(right["virt_dual_seq"]);
long rightCpri = stol(right["pri"]);
long rightCseq = stol(right["virt_cmd_seq"]);

return ( (leftTpri leftTseq (leftTpri==rightTpri && leftTseq==rightTseq && leftCpri (leftTpri==rightTpri && leftTseq==rightTseq && leftCpri==rightCpri &&

leftCseq }
};


I don't like this setup at all--although granted--it works and is
powerful.

I look at BaseTaskMgmt::sortCommands(VectorClass &cmdList) and it just
chaps me that I have to use this "external class"
(sortVectorClassContainerByWXYZ) which seems to dangle around "outside
the box" just so that stable_sort can use it as a predicate in my
BaseTaskMgmt class. Why can't this same simple code in the body of
sortVectorClassContainerByWXYZ::operator() be contained within a
BaseTaskMgmt member function? Can it be?

Otherwise, if I continue to use this same approach (which does work) it
seems that I will leave behind a legacy of "bread crumbs" all over my
classes anytime said class must make use of say an STL sorting
algorithm like stable_sort.

It seems (to me) that there is a "better way" to use such STL
algorithms within classes that does not require creating all sorts of
10-liner function objects (classes/structs) all over, to be used as
predicates.

I have been fumbling around the net tonight for actually quite some
time determined to find a better approach.

I continue to dig up conversations containing references to
mem_fun(_ref) AND bind2nd, which talk about member functions either
without any parameters, or with only 1 parameter. They seem close to
what I am trying to contemplate. In fact, armed with just that
information alone, I tried to write a BaseTaskMgmt::operator()(const
VectorClassRow &left,
const VectorClassRow &right)
....and call it, but only managed to make my MS Visual Studio .NET 2003
compiler very unhappy with my efforts.

Surely there is a much "cooler" way to call a member function:
bool BaseTaskMgmt::sortVectorClassContainerByWXYZ(const &left, const
&right) that accomplishes the same thing as my
sortVectorClassContainerByWXYZ::operator() above???

There HAS to be a way, right? (Even if the "answer" looks terribly
cryptic, and includes binders and/or adapters like bind2nd, mem_fun,
and compose, or something??? (Somebody please lay my curiosity to
rest... please!!!)

-Best Regards,
Darrin


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Fri Oct 28, 2005 9:39 am    Post subject: Re: Can I (should I) transform this Function Object into a m Reply with quote



<darrin.wolf (AT) gmail (DOT) com> wrote:

Quote:

I look at BaseTaskMgmt::sortCommands(VectorClass &cmdList) and it just
chaps me that I have to use this "external class"
(sortVectorClassContainerByWXYZ) which seems to dangle around "outside
the box" just so that stable_sort can use it as a predicate in my
BaseTaskMgmt class. Why can't this same simple code in the body of
sortVectorClassContainerByWXYZ::operator() be contained within a
BaseTaskMgmt member function? Can it be?
class foo

{
public:
bool sort_by_wxyz(const foo &) const;
/* your compare function */
// ...
}

std::vector<foo> foo_container;
std::stable_sort(foo_container.begin(),foo.container.end(),
std::mem_fun_ref(&foo::sort_bt_wxyz)));

looks ok.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
darrin.wolf@gmail.com
Guest





PostPosted: Sat Oct 29, 2005 1:09 pm    Post subject: Re: Can I (should I) transform this Function Object into a m Reply with quote



I don't have the luxury of modifying the class Foo though. So in other
words:
There exists a class FooUser which I wrote. FooUser has a method,
FooUser::sortCommands(Foo &) which must sort all of the records in the
passed in Foo object (which is a class derived from (more or less)
vector< vector.

I'm asking: Isn't there a way that I can put the simple code in
Foo::sort_by_wxyz()(Foo::iterator x&,Foo::iterator y&) in a member
function of FooUser?

This means the only class for sorting implementation would be FooUser,
and I could conceivably derive new classes: DerivedFooUserA,
DerivedFooUserB

This way, each of the derivatives (of a FooUser object) could have an
overloaded version of FooUser::sort_by_wxyz.

This sounds easy enough, but I can't get it to compile. So I am forced
to have all of these "sort_yet_another_way" classes dangling around
which are outside the scope entirely of all FooUser objects.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Valentin Samko
Guest





PostPosted: Sat Oct 29, 2005 8:12 pm    Post subject: Re: Can I (should I) transform this Function Object into a m Reply with quote

[email]darrin.wolf (AT) gmail (DOT) com[/email] wrote:

Quote:
void BaseTaskMgmt::sortCommands(VectorClass &cmdList)
{
stable_sort(cmdList.begin(), cmdList.end(),
sortVectorClassContainerByWXYZ());
}

class sortVectorClassContainerByWXYZ
{
public:
bool operator() (const VectorClassRow &left,
const VectorClassRow &right) const
{
}
};

I look at BaseTaskMgmt::sortCommands(VectorClass &cmdList) and it just
chaps me that I have to use this "external class"
(sortVectorClassContainerByWXYZ) which seems to dangle around "outside
the box" just so that stable_sort can use it as a predicate in my
BaseTaskMgmt class. Why can't this same simple code in the body of
sortVectorClassContainerByWXYZ::operator() be contained within a
BaseTaskMgmt member function? Can it be?

Yes, it can, but why would you want it to be a member function of BaseTaskMgmt
unless it
needs an access to the private/protected members of that class?
You can also save at least one line by declaring your functor class as "struct"
and not
"class", and not using the "public" keyword. Another option is to make it a
function (and
not a functor) and pass pointer to it to stable_sort (considering your
implementation of
this function, I doubt it will be inlined anyway).

Also, it this is the only sorting predicate you need, why not implement operator
< for
VectorClassRow ?

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
darrin.wolf@gmail.com
Guest





PostPosted: Mon Oct 31, 2005 10:47 am    Post subject: Re: Can I (should I) transform this Function Object into a m Reply with quote

Thank you both Carl and Valentin for sharing your knowledge. Carl's
most recent suggestions were eye opening. When I jig his suggestions
at work Tuesday, I will repost the solution I come up with here.

Here are Carl Barron's most recently emailed suggestions that I wish to
share in this forum:
-- quote --
if FooUser only needs one functor object it can be the functor object!
also its operator () (const FooData &,const FoodData &) const can just
forward the args to a virtual bool sort_comp(const FooData &,const
FooData &) like so

#include <vector>
#include <algorithm>

struct FooData
{
int x,y;
FooData() Mad(0),y(0){}
FooData(int a,int b)Mad(a),y(b){};
};


class FooUser
{
typedef std::vector<FooData> Foo;
typedef Foo::iterator Iter;
Foo data;
virtual bool sort_comp(const FooData &,const FooData &)const;
public:
bool operator () (const FooData &x,const FooData &y) {return
sort_comp(x,y);}
void do_sort()
{
std::stable_sort(data.begin(),data.end(),*this);
}
};
-- end quote --


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.