c++ - 继承、指针和软件架构

标签 c++ pointers inheritance architecture member

#include <iostream>

class EquationOfMotion
{
    public:
        // other attributes
        virtual void findNextTimeStep() = 0;
};

class SystemModel
{
    public:
        EquationOfMotion* p_eom;
        // other atributes 
        SystemModel(EquationOfMotion* new_p_eom)
        {
            p_eom = new_p_eom;
        }
};

class VehicleEquationOfMotion: public EquationOfMotion
{
    public: 
        VehicleEquationOfMotion(...){/* initialise attribute*/}
        virtual void findNextTimeStep(){}
};

class Vehicle: public SystemModel
{
 // ???? Implementation ?????
}

VehicleSystemModel 的特例,其中 p_eom 指向 VehicleEquationOfMotion

我想初始化 VehicleEquationOfMotion 的一个实例,并在 Vehicle 中指向它 p_eom。我希望它只定义在Vehicle范围内,同时不使用heap。 甚至可以在不使用堆的情况下将 VehicleEquationOfMotion 对象驻留在 Vehicle 中吗? (如果不是,请指出设计哪里出了问题)。

可能会有帮助:我考虑过 this 中的实现问题但遇到了麻烦(见问题)。

最佳答案

如果我答对了你的问题,那么这样做:

  class FooChild : public FooParent
  {
  public:
      FooChild (int pX):m_BarChild(pX), FooParent(&m_BarChild) // point p_barPar to instance of BarChild (i.e. m_BarChild)
      {
      }
  private:
      BarChild m_BarChild; // instance of BarChild resided in the stack(not the heap) and is local to FooChild
  }

关于c++ - 继承、指针和软件架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743967/

相关文章:

c++ - 为什么以及什么时候我不应该杀死一个线程?

Java 数组移位引用

javascript - 如何创建 Javascript Function 类的子类?

c++ - (C++) 需要数据库方面的帮助

c++ - 嵌入式 web 控件 (IWebBrowser2),嵌入式 javascript 的 onkeydown 和 onkeyup 不触发

c++ - Qt4.8中处理鼠标滚轮事件

c++ - 哪个是整数除以 2 的更好选择?

c 读取字符串输入错误

c - C语言中Struct和Pointer的问题

r - 如何为内部泛型的方法添加额外的参数?