Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing pointer to member function to different member function that then calls for_each

Reply
Thread Tools

Passing pointer to member function to different member function that then calls for_each

 
 
pookiebearbottom@yahoo.com
Guest
Posts: n/a
 
      05-19-2005
Ok, this is what I am trying to do. I have a
member function that as a parameter accepts a
pointer to a different member function. In this 2nd
function, I want to iterate over an STL container
and call the passed in member function. I can do
this in my own loop, but I am looking to call
for_each. I am a little confused on the syntax to
use. Any help would be appreciated.

look at the following code and at the comment in
the printHold function

thanks in advance



#include <iostream>
#include <vector>
#include <string>

typedef std::vector<std::string> Holder;

class Test
{
public:
typedef void (Test::*PRINTFUNC)(std::string);
void printHold(Holder &hold, PRINTFUNC prn)
{
for(Holder::iterator i=hold.begin();i!=hold.end();++i)
(this->*prn)(*i);

// ************** LOOK HERE *****************
// want to do something like this
// for_each(hold.begin(),hold.end(),this->*prn);
// ************** LOOK HERE *****************
}

// not always just a print function
void print(std::string s)
{
cout << s;
}

void printFunc(Holder &hold)
{
printHold(hold,&Test:rint);
}

};

int main(int argc,char *argv[])
{
Holder h;
h.push_back("a");
h.push_back("b");

Test t;
t.printFunc(h);

return 0;
}

 
Reply With Quote
 
 
 
 
John Dibling
Guest
Posts: n/a
 
      05-19-2005
Check out std:tr_fun, std::mem_fun, and related functors.

</dib>

 
Reply With Quote
 
 
 
 
pookiebearbottom@yahoo.com
Guest
Posts: n/a
 
      05-19-2005
So what you are saying is that you do not know the answer?

This is NOT the standard question, but thanks for "trying"

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      05-19-2005
Well, this turned out to be an interesting problem. A very interesting
problem indeed. The solution was not easily found. But I spent
several hours on it, and once I understood the nature of the problem
itself, I could figure out the best solution. I've come up with one
solution that I believe is pretty close to optimal - although I need to
do more testing, I am confident that the basis to the solution is
sound. Even failing that, I have also come up with alternate solutions
that are less than optimal, but certian to work.

Unfortunately for you, since you saw fit to be a pompous jerk in your
reply, you'll have to figure it out on your own.

Take care

</dib>

 
Reply With Quote
 
pookiebearbottom@yahoo.com
Guest
Posts: n/a
 
      05-19-2005
So what you are saying is that you do not know the answer?

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      05-19-2005
And yet you continue to be a pompous jerk.

I do know the answer.

Take care.

</dib>

 
Reply With Quote
 
pookiebearbottom@yahoo.com
Guest
Posts: n/a
 
      05-20-2005
well keep on working on "optimizing" a "syntax error".

"And yet you continue to be a pompous jerk", funny that is what
everyone here is saying about you.

Do you get extra credit with your programming 101 teacher with every
"answer" you give on the web? Do you get more credit for getting it
correct? Thanks for answering, but if you are not going to read
questions and just put "read the faq" when you have no idea what the
question even is, then do not waste the bandwidth.

I have read your past posts and you seem to help people, but I guess
you have just become "one of those guys" that just get annoyed at
people that do not know what you know and you reply to every question
with "just read the manual idiot", when if you already know the answer
you can find it, but if you do not, then you can't. Just take a break
and maybe you will come back one day without the attitude.

 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      05-20-2005
> Thanks for answering, but if you are not going to read
> questions and just put "read the faq" when you have no idea what the
> question even is, then do not waste the bandwidth.


I did read the answer. Actually, I read it wrong. You're right about
that, and I did give you the wrong answer.

Originally when I gave you the wrong answer, you had a choice. You
could choose to be cool about it, and ask me to look at the problem
again. You could be sympathetic to the fact that I may have made an
honest mistake, even though I was genuinely trying to be helpful. You
might be constructive and enlist my help further.

Or you could be a jerk. Would you have said, "So what you are saying
is that you do not know the answer?" to your mother? Then you
shouldn't say it here. Just becasue you are anonymously hidden behind
some unknown email account doesn't make it OK to behave like a child.

You chose to be a jerk. If you choose to act like a child, I'll be
more than happy to treat you like one. I see no reason to help you.

 
Reply With Quote
 
PJP of NYC
Guest
Posts: n/a
 
      05-24-2005
this works for me

change:
for_each(hold.begin(),hold.end*(),this->*prn);

to:
for_each(hold.begin(),hold.end(),
std::bind1st(std::mem_fun(prn), this));

Of course I am still optimizing it.

John Dibling wrote:
> Check out std:tr_fun, std::mem_fun, and related functors.
>
> </dib>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
for_each and invoking self's member pointer Kenneth Porter C++ 8 10-14-2007 12:32 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments