0
#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 is a specialization of SystemModel where p_eom points to VehicleEquationOfMotion.

I would like to initialise, an instance of VehicleEquationOfMotion and point to it p_eom in Vehicle. I want it to be defined only within the scope of Vehicle, and at the same time, not to use heap. Is it even possible to reside VehicleEquationOfMotion object inside Vehicle without using the heap? (If not, please suggest where the design has gone wrong).

Might be helpful: I thought about the implementation in this question but ran into trouble (see the question).

Community
  • 1
  • 1
aiao
  • 4,621
  • 3
  • 25
  • 47

4 Answers4

1

If I got your question correctly, then do it like 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
  }
TravellingGeek
  • 1,581
  • 11
  • 16
  • what about the slicing problem? – aiao Apr 01 '13 at 13:41
  • there is no slicing problem here, because your p_barPar is a pointer to BarParent not an instance, so in this, we are letting p_barPar from BarParent points to the address of m_BarChild in FooChild – TravellingGeek Apr 01 '13 at 13:45
  • slicing problem happens when we assign m_BarChild to an instance of BarParent(not pointer). for example: if p_barPar is an instance not a pointer – TravellingGeek Apr 01 '13 at 13:47
  • With this implementation, you are forced to hardcode an initialization value to m_BarChild, aren't you? – Subway Apr 01 '13 at 14:09
  • @Subway - that problem is easily modifiable by putting the m_BarChild in the initialization list, it goes first before the FooParent, check the code I've edited it – TravellingGeek Apr 02 '13 at 00:53
0

If you want to have FooParent.p_barPar to be pointing to a BarChild that resides inside FooChild, you might need to add a default ctor to FooParent and a method as follows as well: set_p_barPar(BarChild* new_p_bar){p_barPar = new_p_bar;}. So you get:

class FooParent
{
    public:
        BarParent* p_barPar;
        FooParent (){}
        FooParent (BarChild* new_p_bar)
        {
            p_barPar = new_p_bar;
            std::cout << p_barPar->x << std::endl;
        }
    protected:
        set_p_barPar(BarChild* new_p_bar)
        {
            p_barPar = new_p_bar;
        }
}

Then you can implement FooChild:

class FooChild : public FooParent
{
     public:
          FooChild(int new_x, BarChild* new_p_bar):_bar_child(new_x)
          {
               set_p_barPar(&_bar_child);
          }

     private:     //? Depends on your plans
         BarChild _bar_child();
}
Subway
  • 5,286
  • 11
  • 48
  • 59
0

Use a class template.

class EquationOfMotion { ... };

template <typename EOM>
class SystemDynamics
{
    EOM EquationOfMotion;
    ...
};

class VehicleEquationOfMotion : public EquationOfMotion { ... };

class Vehicle : public SystemDynamics<VehicleEquationOfMotion> { ... };
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

May be this is what you want. But the design is not safe. You are passing the pointer to a uninitialized object.

class Vehicle: public SystemModel
{
public:
    Vehicle(): SystemModel(&_vem)
    {

    }

    VehicleEquationOfMotion _vem;
}

However, it is safer to do the following:

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

class Vehicle: public SystemModel
{
   public:
   Vehicle(): SystemModel(&_vem)
   {
      p_eom = &_vem;
   }
   VehicleEquationOfMotion _vem;
};
Sharath
  • 1,627
  • 2
  • 18
  • 34