Passing a member function as a parameter of a member function

Discussion in 'C++' started by Azdo, Mar 6, 2007.

  1. Azdo

    Azdo Guest

    Hi,

    is it possible to pass a member function pointer to other member
    function of the same class in order to get different behaviours?
    Something like

    class A
    {

    int m(int d) /* First way */
    {
    return d;
    }

    int n(int d) /*Second way*/
    {
    return d+2;
    }

    /* This does the calculations depending on the function passed */

    void f(int (*g)(int))
    {
    std::cout << g(8) << "\n";
    }

    void start()
    {
    f(&m); /* Do it the "m" way */
    f(&n); /* Do it the "n" way */
    }
    };

    What I am trying to accomplish is to have a member function that can
    call different procedures to do slightly different jobs.

    Thank you,
     
    Azdo, Mar 6, 2007
    #1
    1. Advertising

  2. Azdo a écrit :
    > Hi,
    >
    > is it possible to pass a member function pointer to other member
    > function of the same class in order to get different behaviours?
    > Something like
    >
    > class A
    > {
    > int m(int d) /* First way */
    > [snip]
    > int n(int d) /*Second way*/
    >[snip]
    >
    > /* This does the calculations depending on the function passed */
    >
    > void f(int (*g)(int)
    > [snip]
    > void start()
    > {
    > f(&m); /* Do it the "m" way */
    > f(&n); /* Do it the "n" way */
    > }
    > };
    >
    > What I am trying to accomplish is to have a member function that can
    > call different procedures to do slightly different jobs.


    The declation of f() would take a pointer on A's member function:
    void f(int (A::*g)(int));
    {
    std::cout << this->*g(8) << "\n";
    }

    and
    f(&A::m);
    f(&A::n);


    Michael
     
    Michael DOUBEZ, Mar 6, 2007
    #2
    1. Advertising

  3. Azdo

    Azdo Guest

    On 6 mar, 12:13, Michael DOUBEZ <> wrote:
    > Azdo a écrit :
    >
    >
    >
    > > Hi,

    >
    > > is it possible to pass a member function pointer to other member
    > > function of the same class in order to get different behaviours?
    > > Something like

    >
    > > class A
    > > {
    > > int m(int d) /* First way */
    > > [snip]
    > > int n(int d) /*Second way*/
    > >[snip]

    >
    > > /* This does the calculations depending on the function passed */

    >
    > > void f(int (*g)(int)
    > > [snip]
    > > void start()
    > > {
    > > f(&m); /* Do it the "m" way */
    > > f(&n); /* Do it the "n" way */
    > > }
    > > };

    >
    > > What I am trying to accomplish is to have a member function that can
    > > call different procedures to do slightly different jobs.

    >
    > The declation of f() would take a pointer on A's member function:
    > void f(int (A::*g)(int));
    > {
    > std::cout << this->*g(8) << "\n";
    >
    > }
    >
    > and
    > f(&A::m);
    > f(&A::n);
    >
    > Michael


    Great! I had however to do a tiny change for that to work:

    std::cout << (this->*g)(8) << "\n";

    Thank you very much Michael.
     
    Azdo, Mar 6, 2007
    #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. Newsgroup - Ann
    Replies:
    5
    Views:
    610
    John Carson
    Jul 30, 2003
  2. Replies:
    8
    Views:
    557
    PJP of NYC
    May 24, 2005
  3. sun
    Replies:
    2
    Views:
    3,072
  4. Mister B
    Replies:
    8
    Views:
    554
    Nick Keighley
    Aug 26, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    181
Loading...

Share This Page