#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 ?????
}
Vehicle
是 SystemModel
的特例,其中 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/