Search code examples
c++odeint

composition constructor get variable from member variables


I am calling a class (e.g :B) as an argument to another class (e.g: A)(composition). I want to tell class B to get your constructor variable from member variable of the class A.

This is an example from boost odeint:

lib.h

using namespace boost::numeric::odeint;

/* The type of container used to hold the state vector */
typedef vector<double> state_type;

/* The rhs of x' = f(x) defined as a class */
class harm_osc {
    double m_gam;
public:
    harm_osc( double gam ) : m_gam(gam) { }

    void operator() ( const state_type &x , state_type &dxdt , const double /* t */ )
    {
        dxdt[0] = x[1];
        dxdt[1] = -x[0] - m_gam*x[1];
    }
};
/*------------------------------------------------------------*/
class osc_solver {


    public:
    osc_solver(const harm_osc &ho) : m_ho(ho) {
        x = {1.0, 0.0}; // start at x=1.0, p=0.0

    }
    void run();

    private:
    harm_osc m_ho;
    state_type x;
    vector<state_type> x_vec;
    vector<double> times;
};

lib.cpp

void osc_solver::run()
{
    size_t steps = integrate(m_ho,
                             x, 0.0, 10.0, 0.1,
                             push_back_state_and_time(x_vec, times));

    /* output */
    for (size_t i = 0; i <= steps; i++)
    {
        cout << times[i] << '\t' << x_vec[i][0] << '\t' << x_vec[i][1] << '\n';
    }
}

main.cpp

int main(int /* argc */ , char** /* argv */ )
{
    osc_solver sol(harm_osc(0.15));
    sol.run();
    return 0;
}

I need something like this:

osc_solver sol(0.15, harm_osc));

because sometimes I need to pass many variables to the classes that I use in both of them.

Thanks for any guide.


Solution

  • You could define the osc_solver class with a template parameter HarmType, which parameterizes the type of the member osc_solver::m_ho and construct it by forwarding the arguments of the constructor of osc_solver. Something like

    #include <utility>
    
    template <class HarmType>
    class osc_solver {
    public:
         template <class... ArgsType>
         osc_solver(ArgsType&&... parameters_ham) : m_ho(std::forward<ArgsType>(parameters_harm)...) {
            x = {1.0, 0.0}; // start at x=1.0, p=0.0
    
        }
        void run();
    
        private:
        HarmType m_ho;
        state_type x;
        vector<state_type> x_vec;
        vector<double> times;
    };
    

    Then for example, you could use an object of osc_solver as

    // harm_osc needs 1 parameter in the constructor
    osc_solver<harm_osc> solver(0.15);
    // harm_osc_special needs 2 parameters in the constructor
    osc_solver<harm_osc_special> solver(0.15, 0.2);
    

    More generally, you could define osc_solver::osc_solver as to require both parameters used in class osc_solver and in constructing osc_solver:m_ho, like:

    template <class HarmType>
    class osc_solver {
    public:
        template <class... ArgsType>
        osc_solver(double a, ArgsType&&... parameters_ham) : m_ho(std::forward<ArgsType>(a, parameters_harm)...)
        {
           // Here use parameter a
        }
     // ...