How to pass a method as parameter to another method [???]

Hello,
I saw that in C I can pass a function as argument to another function. So, I tried to pass a function as argument to a method, in C++, and I got. For the next step I did a little search on internet, but I found nothing that could help me to pass a method as parameter to another method. So I'm coming here to see if anybody here can help me.

I suspect that there is a difference in the case of methods from the same class and methods from different classes. I wonder how to define and call the method that receives the another method.

Thanks!
Do you mean pass a pointer to a function ??
No!

method = member function

I mean how to pass a member function as argument to another member function.
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A{
          public:
              A();     //constructor
             void methA1();
             void methA2(/*this member function receives  methA1() as parameter*/);
};

class B{
          public:
              B();    //constructor
            void methB(/*this member function recives methA1(), from another class, as parameter*/);
};

//Example of usage
int main()
{
      A a;   //declaring object a, from class A.
      a.methA2(/*a calls methA2 which has methA1 as argument*/);

      B b;   //declaring object b, from class B.
      b.methB(/*b calls methB which has methA1, from another class, as argument*/);

      return 0;
}


I think this example solve the doubts from previous post, but...

Using the Euler numerical method as example: (info about this method -> http://en.wikipedia.org/wiki/Euler_method )

We have:

y[i+1] = y[i] +h*f(x[i],y[i])


I can create a member funcion Euler(...) and calculate an approximate value y[n] for one ODE, which is represented by the member function f(x,y), but I may need to change this ODE by another one, or I need to solve two ODEs in the same program. So I would like to do this just by changing one argument from the member function Euler(...).

Did you understood?

English isn't my native language, so I hope that my mistakes in this language don't affect your understanding of my doubt.
Here we use to call member functions by "métodos" so I translate as method, a false friend, I think.

Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
class A {
   public:
       void methA1();
       void methA2( void (A::*pmemfn)() )
           { (this->*pmemfn)(); }
};

class B {
    public:
        void methB( void (A::*pmemfn)(), A& obj )
            { (obj.*pmemfn)(); }
};

jsmith,
Sorry the incovenience (and curiosity), but how I call these member function in my example program?

1
2
3
4
5
6
7
8
9
10
11
//Example of usage
int main()
{
      A a;   //declaring object a, from class A.
      a.methA2(/*What I'll put here???*/);

      B b;   //declaring object b, from class B.
      b.methB(/*What I'll put here???*/);

      return 0;
}


Another question, I'm obliged to declare objects from both classes? I mean, could I call methB() without declare any object from class A?

Thanks!
1
2
3
4
5
6
7
8
9
10
11
//Example of usage
int main()
{
      A a;   //declaring object a, from class A.
      a.methA2(&A::methA1); //just get the address like a normal function

      B b;   //declaring object b, from class B.
      b.methB(&A::methA1);

      return 0;
}


And no, you couldn't in this case. Since A::methA1 requires an object (because it is not static), you must call it with an object. If the function were static, it works similarly to a function inside a namespace (or another global function really).
Topic archived. No new replies allowed.