How to pass derived object in function as base obj

2020-05-03 11:52发布

I was trying to do something like this.

class ODESolver
{
public:
    // Fourth-order Runge-Kutta ODE solver.
    static void RungeKutta4(ODE ode, double stepSize)
    {
        // Define some convenience variables to make the
        // code more readable
        int j;
        int numEqns = ode.getNumberOfEquations();
        double s;
        double *q;
        double *dq1 = new double[numEqns];
        double *dq2 = new double[numEqns];
        double *dq3 = new double[numEqns];
        double *dq4 = new double[numEqns];

        // Retrieve the current values of the dependent
        // and independent variables.
        s = ode.getIndependentVar();
        q = ode.getAllIndependentVariables();

        dq1 = ode.getRightHandSide(s, q, q, stepSize, 0.0);
        dq2 = ode.getRightHandSide(s + 0.5*stepSize, q, dq1, stepSize, 0.5);
        dq3 = ode.getRightHandSide(s + 0.5*stepSize, q, dq2, stepSize, 0.5);
        dq4 = ode.getRightHandSide(s + stepSize, q, dq3, stepSize, 1.0);

        // Update the dependent and independent variable values
        // at the new dependent variable location and store the
        // values in the ODE object arrays.
        ode.setIndependentValue(s + stepSize);

        for (j = 0; j < numEqns; j++)
        {
            q[j] = q[j] + (dq1[j] + 2.0*dq2[j] + 2.0*dq3[j] + dq4[j]) / 6.0;
            ode.setDependentValue(q[j], j);
        }
    }
};

class ODE
{
    // Declare fields used by the class
private:
    int numEqns; // number of equations to solve
    double *dependent; // array of dependent variables
    double independent; // independent variable
    // Constructor
public :
    // Constructor
    ODE:: ODE(int numEqns) {
        this->numEqns = numEqns;
        dependent = new double[numEqns];


    virtual double* getRightHandSide(double IndependentVariable, double DependentVariables[],
    double DeltaOfDependents[], double DIndependent, double DependentScale);

};

class SimpleProjectile : public ODE {

public:
    // Gravitational acceleration.
    static double Gravity;

    SimpleProjectile::SimpleProjectile(double x0, double y0, double vx0, double vy0,
        double t);

    // These methods return the location, velocity,
    // and time values.
    double getVx();
    double getVy();
    double getX();
    double getY();
    double getTime();

    // This method updates the velocity and position
    // of the projectile according to the gravity-only model.
    void updateLocationAndVelocity(double dt);

    // Because SimpleProjectile extends the ODE class,
    // it must implement the getRightHandSide method.
    // In this case, the method returns a dummy array.
    double* getRightHandSide(double s, double Q[],
    double deltaQ[], double ds, double qScale)
};


void SimpleProjectile::updateLocationAndVelocity(double dt)
{
    double time = getIndependentVar();
    double vx0 = getDependentVar(0);
    double x0 = getDependentVar(1);
    double vy0 = getDependentVar(2);
    double y0 = getDependentVar(3);

    // Update the xy locations and the y-component
    // of velocity. The x-velocity doesn't change.
    double x = x0 + vx0*dt;
    double vy = vy0 + Gravity*dt;
    double y = y0 + vy0*dt + 0.5*Gravity*dt*dt; // s = S0 + (V0 * t + 1/2 a*t^2)

    //Update Time
    time = time + dt;

    //Load new values into ODE 
    setIndependentValue(time);
    setDependentValue(x, 1);
    setDependentValue(y, 4);
    setDependentValue(vy, 3);
}

class DragProjectile : public SimpleProjectile {

private : 
    double mass, area, density, Cd;

public:
    DragProjectile::DragProjectile(double x0, double y0,
    double vx0, double vy0, double time,
    double mass, double area, double density, double Cd);

    // These methods return the value of the fields
    // declared in this class.
    double getMass();
    double getArea();
    double getDensity();
    double getCd();

    // This method updates the velocity and location
    // of the projectile using a 4th order Runge-Kutta
    // solver to integrate the equations of motion.
    void updateLocationAndVelocity(double dt);

    double* getRightHandSide(double IndependentVariable, double DependentVariables[],
        double DeltaOfDependents[], double DIndependent, double IndependentScale);
};
void DragProjectile::updateLocationAndVelocity(double dt)
{
    ODESolver::RungeKutta4(this, dt); // this is where problem comes in
}

now i want to pass the DragProjectile in the ODESolver::RungeKutta4 as ODE Object because its derived class of SimpleMotion and SimpleMotion is derived off ODE class. I tried upcasting, but i dont find a work around for keyword : "this"

标签: c++ oop
3条回答
Luminary・发光体
2楼-- · 2020-05-03 12:18

You do realize that this

static void method1(equation eq, double dt);

is expecting a object of equation instead of pointer to eq

so if you just change this method to

static void method1(equation* eq, double dt);

the following should work as is.

equationSolver::method1(this, dt); // i am getting error here...

because simpleMotion IS_A equation

查看更多
虎瘦雄心在
3楼-- · 2020-05-03 12:23

this is a pointer, but your function expects an object. So dereference the pointer to get the object:

equationSolver::method1(*this, dt);
                        ^

I suspect that the function should probably take the equation by reference (or maybe even pointer) rather than value; but that's just a hunch based on little information.

查看更多
男人必须洒脱
4楼-- · 2020-05-03 12:43

Shouldn't it be

equationSolver::method1(*this, dt);

Or better yet, make that function to accept a pointer:

class equationSolver{
public:
    static void method1(equation* eq, double dt);
}

Then your original call should work.

Update:

Also you probably want public inheritance:

class simpleMotion : public equation
查看更多
登录 后发表回答