473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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;
}

Jul 23 '05 #1
8 1426
Check out std::ptr_fun, std::mem_fun, and related functors.

</dib>

Jul 23 '05 #2
So what you are saying is that you do not know the answer?

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

Jul 23 '05 #3
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>

Jul 23 '05 #4
So what you are saying is that you do not know the answer?

Jul 23 '05 #5
And yet you continue to be a pompous jerk.

I do know the answer.

Take care.

</dib>

Jul 23 '05 #6
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.

Jul 23 '05 #7
> 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.

Jul 23 '05 #8
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::ptr_fun, std::mem_fun, and related functors.

</dib>


Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
5
by: BCC | last post by:
In looking through some code I have inherited, I notice a lot of places where the programmer used operator() as a function call: void operator() (int x, int y); Rather than an explicit...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
13
by: arnuld | last post by:
i see the use of pointers, from K&R2 but what is the use of: 1. "pointer to pointer": char c; char** ppc; 2. pointer to function:
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.