如何在C++中将派生对象作为父对象传递?

Muh*_*zan -2 c++ oop inheritance

我正在使用核心OOP模型.我在哪里遇到一些问题.

使用"this"关键字.这是我想要实现的示例代码.

class equation {
// some properties
};
class equationSolver{
 public:
    static void method1(equation eq, double dt);
}
class simpleMotion : public equation{};
class DragProjectile : public simpleMotion{
void updateVariables(double time){
    equationSolver::method1(this, time); // I am getting error cannot convert 
    // "DragProjectile" to "equation"
}
Run Code Online (Sandbox Code Playgroud)

如果你能提供帮助,我会很高兴的.提前致谢

Joh*_*nck 5

您需要传递,*this因为您的方法采用equationby值.顺便说一下,您可能希望更改method1()为以equation类似equation& eq或const equation&替代方式引用,以避免副本并保留您可能具有的任何虚拟方法的行为(阅读"切片").