functors .

Discussion in 'C++' started by vsgdp, Sep 10, 2005.

  1. vsgdp

    vsgdp Guest

    Hi,

    I need to pass a member function of the form C::foo(B* b, A& a) to a generic
    function:

    template<typename Func>
    void CLASS2::bar(Func f)
    {
    ...

    f(b, a);
    }


    void C::g()
    {
    ....

    class2->bar( C::foo );
    }

    It does not work. I tried mem_fun, but that also did not work. Effective
    STL page 175 gives an example of mem_fun when the function takes no
    parameters.

    error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result
    (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for
    'overloaded function type' from 'overloaded function type'
    vsgdp, Sep 10, 2005
    #1
    1. Advertising

  2. vsgdp

    benben Guest

    "vsgdp" <> wrote in message
    news:N7tUe.6254$mH.3663@fed1read07...
    > Hi,
    >
    > I need to pass a member function of the form C::foo(B* b, A& a) to a
    > generic function:
    >
    > template<typename Func>
    > void CLASS2::bar(Func f)
    > {
    > ...
    >
    > f(b, a);
    > }
    >
    >
    > void C::g()
    > {
    > ...
    >
    > class2->bar( C::foo );
    > }
    >
    > It does not work. I tried mem_fun, but that also did not work. Effective
    > STL page 175 gives an example of mem_fun when the function takes no
    > parameters.
    >
    > error C2784: 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result
    > (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument
    > for 'overloaded function type' from 'overloaded function type'
    >
    >



    The easiest way is to look at the function template from boost.

    Ben
    benben, Sep 10, 2005
    #2
    1. Advertising

  3. vsgdp

    Rolf Magnus Guest

    vsgdp wrote:

    > Hi,
    >
    > I need to pass a member function of the form C::foo(B* b, A& a) to a
    > generic function:


    You cannot pass a function, you can only pass a pointer to it.

    >
    > template<typename Func>
    > void CLASS2::bar(Func f)
    > {
    > ...
    >
    > f(b, a);
    > }
    >
    >
    > void C::g()
    > {
    > ...
    >
    > class2->bar( C::foo );


    class2->bar( &C::foo );

    > }
    >
    > It does not work.


    Well, how would the compiler know which object to call the member function
    for? You have to specify that. The syntax for calling a member function
    through pointer is:

    (object.*function)(parameters);

    or for a pointer to the object:

    (object->*function)(parameters);
    Rolf Magnus, Sep 10, 2005
    #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. Paul MG
    Replies:
    2
    Views:
    382
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    339
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    311
    red floyd
    Nov 13, 2003
  4. nsgi_2004

    functors

    nsgi_2004, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    453
    Karl Heinz Buchegger
    Aug 12, 2004
  5. Satish
    Replies:
    1
    Views:
    769
    David Hilsee
    Sep 10, 2004
Loading...

Share This Page