mem_fun_ref

Discussion in 'C++' started by Anders, Jun 30, 2003.

  1. Anders

    Anders Guest

    Hello.
    If I was to call a method of class T, M for each element in a collection,
    I'd do it like this:

    vector<int> vec;
    ....
    for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));

    Okay, I can see that mem_fun_REF makes it obvious that I must use a
    reference, but why? Is it not possible to pass a function by-reference? As I
    see it, T::M is being passed as a pointer here.

    void func(int& val); func(12); << reference
    void func(int* val); int val = 12; func(&val); << pointer

    //Anders
     
    Anders, Jun 30, 2003
    #1
    1. Advertising


  2. > Hello.
    > If I was to call a method of class T, M for each element in a collection,
    > I'd do it like this:
    >
    > vector<int> vec;
    > ...
    > for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));
    >
    > Okay, I can see that mem_fun_REF makes it obvious that I must use a
    > reference, but why? Is it not possible to pass a function by-reference?


    No, you can only pass the address.
    > As I see it, T::M is being passed as a pointer here.


    The REF, in mem_fun_ref, refers to the argument type( of T::M ), which
    will be passed to the function call operator.
    >
    > void func(int& val); func(12); << reference
    > void func(int* val); int val = 12; func(&val); << pointer
    >
    > //Anders
    >
    >
     
    Kurt Krueckeberg, Jun 30, 2003
    #2
    1. Advertising

  3. Anders

    tom_usenet Guest

    On Mon, 30 Jun 2003 22:34:24 +0200, "Anders" <>
    wrote:

    >Hello.
    >If I was to call a method of class T, M for each element in a collection,
    >I'd do it like this:
    >
    >vector<int> vec;
    >...
    >for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M));


    That can't be legal - you can't call a T member function on an int
    reference.

    >
    >Okay, I can see that mem_fun_REF makes it obvious that I must use a
    >reference, but why? Is it not possible to pass a function by-reference? As I
    >see it, T::M is being passed as a pointer here.
    >
    >void func(int& val); func(12); << reference
    >void func(int* val); int val = 12; func(&val); << pointer


    The ref refers to the the fact that the object passed as the first
    argument of the functor must be a reference. e.g.

    T t;

    mem_fun_ref(&T::M)(t); //pass T reference

    mem_fun(&T::M)(&t); //pass T pointer

    so in practice you use mem_fun_ref for containers like:
    vector<T>
    and mem_fun for containers like:
    vector<T*>

    Tom
     
    tom_usenet, Jul 1, 2003
    #3
    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. Jan Rendek
    Replies:
    0
    Views:
    500
    Jan Rendek
    Jul 15, 2003
  2. Michiel Salters

    Re: How to use bind2nd with mem_fun_ref?

    Michiel Salters, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    414
    Michiel Salters
    Jul 16, 2003
  3. Raoul Gough

    Re: mem_fun_ref with 1 argument?

    Raoul Gough, Aug 26, 2003, in forum: C++
    Replies:
    0
    Views:
    636
    Raoul Gough
    Aug 26, 2003
  4. Ahti Legonkov

    Re: mem_fun_ref with 1 argument?

    Ahti Legonkov, Aug 26, 2003, in forum: C++
    Replies:
    0
    Views:
    483
    Ahti Legonkov
    Aug 26, 2003
  5. Ioannis Vranos

    mem_fun vs mem_fun_ref

    Ioannis Vranos, Mar 17, 2005, in forum: C++
    Replies:
    13
    Views:
    7,026
    Pete Becker
    Mar 17, 2005
Loading...

Share This Page