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:rint);
}
};
int main(int argc,char *argv[])
{
Holder h;
h.push_back("a");
h.push_back("b");
Test t;
t.printFunc(h);
return 0;
}
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;
}