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

Discussion in 'C++' started by pookiebearbottom@yahoo.com, May 19, 2005.

  1. Guest

    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;
    }
     
    , May 19, 2005
    #1
    1. Advertising

  2. John Dibling Guest

    Check out std::ptr_fun, std::mem_fun, and related functors.

    </dib>
     
    John Dibling, May 19, 2005
    #2
    1. Advertising

  3. Guest

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

    This is NOT the standard question, but thanks for "trying"
     
    , May 19, 2005
    #3
  4. John Dibling Guest

    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>
     
    John Dibling, May 19, 2005
    #4
  5. Guest

    So what you are saying is that you do not know the answer?
     
    , May 19, 2005
    #5
  6. John Dibling Guest

    And yet you continue to be a pompous jerk.

    I do know the answer.

    Take care.

    </dib>
     
    John Dibling, May 19, 2005
    #6
  7. Guest

    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.
     
    , May 20, 2005
    #7
  8. John Dibling Guest

    > 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.
     
    John Dibling, May 20, 2005
    #8
  9. PJP of NYC Guest

    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>
     
    PJP of NYC, May 24, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    610
    John Carson
    Jul 30, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,052
    Fraser Ross
    Aug 14, 2004
  3. andy6
    Replies:
    2
    Views:
    761
    andy6 via DotNetMonster.com
    Jun 9, 2006
  4. Kenneth Porter
    Replies:
    8
    Views:
    418
    Kai-Uwe Bux
    Oct 14, 2007
  5. Replies:
    0
    Views:
    295
Loading...

Share This Page