Get this example to work with member functions?

Discussion in 'C++' started by JohanS, Aug 23, 2004.

  1. JohanS

    JohanS Guest

    template<typename T>
    T testFunc( T(*pFunction) (T), T x)
    {
    return pFunction(x);
    }

    int print(int i)
    {
    return i;
    }

    int main()
    {
    cout << testFunc(&print , 5) << endl;
    return 0;
    }

    How can i make this work calling a member function instead?
    If i have object A with member function int setValue(int);
    and i wanna pass &A.setValue ?
    JohanS, Aug 23, 2004
    #1
    1. Advertising

  2. JohanS wrote:
    >
    > template<typename T>
    > T testFunc( T(*pFunction) (T), T x)
    > {
    > return pFunction(x);
    > }
    >
    > int print(int i)
    > {
    > return i;
    > }
    >
    > int main()
    > {
    > cout << testFunc(&print , 5) << endl;
    > return 0;
    > }
    >
    > How can i make this work calling a member function instead?
    > If i have object A with member function int setValue(int);
    > and i wanna pass &A.setValue ?


    See the FAQ

    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 23, 2004
    #2
    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. Hamish
    Replies:
    3
    Views:
    553
    Alf P. Steinbach
    Jan 25, 2008
  2. flounder
    Replies:
    4
    Views:
    497
    flounder
    Apr 2, 2009
  3. Hicham Mouline
    Replies:
    0
    Views:
    420
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    404
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    699
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page