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

P

pookiebearbottom

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::print);
}

};

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

Test t;
t.printFunc(h);

return 0;
}
 
P

pookiebearbottom

So what you are saying is that you do not know the answer?

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

John Dibling

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>
 
J

John Dibling

And yet you continue to be a pompous jerk.

I do know the answer.

Take care.

</dib>
 
P

pookiebearbottom

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.
 
J

John Dibling

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.
 
P

PJP of NYC

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.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top