如何将函数中的派生对象作为C++中的基对象传递

How to pass derived object in function as base object in C++

本文关键字:对象 C++ 派生 函数      更新时间:2023-10-16

我正试图做这样的事情。

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
}

现在我想将ODESolver:RungeKutta4中的DragProjectile传递为CODE对象,因为它的派生类SimpleMotionSimpleMotion都是从ODE类派生的。我尝试了升级,但找不到关键字"this"的变通方法:

this是一个指针,但函数需要一个对象。因此,取消引用指针以获取对象:

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

我怀疑函数可能应该通过引用(甚至指针)而不是值来获取equation;但这只是基于少量信息的预感。

您确实意识到这个

static void method1(equation eq, double dt);

正在期待一个等式对象,而不是指向eq 的指针

所以如果你把这个方法改成

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

以下应该按原样工作。

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

因为simpleMotion IS-A方程

不应该是吗

equationSolver::method1(*this, dt);

或者更好的是,让这个函数接受一个指针:

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

那么你原来的电话应该可以用了。

更新:

此外,您可能想要公共继承:

class simpleMotion : public equation