1

I have read a number of posts about passing a pointer to a class method to a different classes method, but couldn't find a clear answer/understand how I should approach my specific problem.

Some that I looked at:

Passing a pointer to a class member function as a parameter

C++ Pass a member function to another class

I would really appreciate help making my particular scenario work.

I have three classes: Eom, Rk4, and Manage. In the first class I have two methods, get_pos and set_pos.

class Eom
{
    public:

        // Get methods
        double get_pos() {return pos;}

        // Set methods
        void set_pos(double pos_in) {pos = pos_in;}

    private:
        double pos;
};

In the second class I have a method called integrate that I want to pass Eom::get_pos and Eom::set_pos method to.

class Rk4
{
    public:

        // Method that takes a method to get_pos 
        // and a method to set_pos as input
        void integrate(Eom *p_get_func, Eom *p_set_func); 
};

The Manage class run method is where I want to create the pointer objects to Rk4 and Eom, and call the Rk4 integrate method.

class Manage
{

    public:

            // Declare pointers
            Eom *p_eom;
            Rk4 *p_rk4;

            // Run method
            void run();

};

Manage::run()
{

    p_eom = new Eom;
    p_rk4 = new Rk4;

    // Call integrate, passing in the Eom get_pos and set_pos functions;
    p_rk4->integrate(p_eom->get_pos, p_rk4->set_pos);

}

I currently get the following errors:

invalid use of non-static member function
p_rk4->integrate(p_eom->get_pos, p_eom->set_pos); 

I would also like to declare the integrate method in a more generic fashion so that I could pass in get/set methods from other classes:

void integrate(Generic_class *p_get_func, Generic_class *p_set_func); 

Thanks for any help in advance!

ndlawrence
  • 23
  • 4
  • Setting aside the correct syntax, you do understand that you can't just take a pointer to a class method, and call it, just like that, like calling some arbitrary function, via a pointer? You must have an instance of that class, at hand, whose method you wish to invoke, indirectly, via the pointer? Because with just those two methods, I don't see what using class methods would accomplish, especially since `integrate()`, which is supposed to receive them, does not appear to have an instance of the class anywhere in its vicinity. – Sam Varshavchik Apr 01 '18 at 01:47
  • I understand that integrate() needs the instance of the class to be able to call the get/set methods, but I am not sure how to accomplish passing the methods as well as the instance of the class.. – ndlawrence Apr 01 '18 at 02:00
  • How do you pass two things to a function (or a class method)? Why, you pass each thing as a separate parameter. – Sam Varshavchik Apr 01 '18 at 02:07

1 Answers1

0

Your code is a bit clunky I guess, here's what I think of, you can encapsulate the functions in a lambda and pass using std::function like so.

#include <iostream>
#include <functional>

class Eom
{
    public:

        // Get methods
        double get_pos() {return pos;}

        // Set methods
        void set_pos(double pos_in) {pos = pos_in;}

    private:
        double pos;
};

class Rk4
{
    public:

        // Method that takes a method to get_pos 
        // and a method to set_pos as input

    void integrate(std::function<double(void)> get,std::function<void(double)> set, double val) 
    {
       int i = get();
       set(val);
    }

};

In the manage class you can use like this:

class Manage
{

    public:

            // Declare pointers
            Eom *p_eom;
            Rk4 *p_rk4;

            // Run method
            void run();

};

void Manage::run()
{

    p_eom = new Eom;
    p_rk4 = new Rk4;
    double val =100;
    // Call integrate, passing in the Eom get_pos and set_pos functions;
    std::function<double(void)> a = [=]()->double { return p_eom->get_pos(); };
    std::function<void(double)> b = [=](double d)->void { return p_eom->set_pos(d); };
    p_rk4->integrate(a,b,val);

} 
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • This works! How could I archive the same result in a less "clunky" way? I.e., I need "integerate()" to be able to retrieve/set specified class attributes. Presumably I could pass a pointer to the specific attribute, i.e. pos, and directly get/set it from integrate() instead of using get/set methods, but I am under the impression it is bad practice to directly alter a class's attributes? – ndlawrence Apr 01 '18 at 02:34
  • @ndlawrence: In this case, passing function seems more like beating in the bush, directly calling the member function should be the way to go. – seccpur Apr 01 '18 at 02:45