title: Polymorphism in C++ with a vector with base classes and reference_wrapper
body: <p>I need to use polymorphism in C++ for objects which are stored in a vector. </p>
  
  <p>Understand from other questions that the objects in the vector need to be stored by reference since otherwise object slicing occurs.</p>
  
  <p>My understanding is that std::reference_wrapper would be a good fit, but I'm having some issues implementing it. </p>
  
  <p>ActorRecordingItem is the base class and ActorVehicleEnterRecordingItem is an example of a class which needs polymorphism  (inherts from ActorRecordingItem via ActorVehicleRecordingItem)</p>
  
  <pre><code>    class ActorRecordingItem {
  protected:
      DWORD m_ticksAfterRecordStart;
      Ped m_actorPed;
      Vector3 m_location;
      DWORD m_ticksDeltaCheckCompletion;
  public:
      ActorRecordingItem(DWORD ticksStart, Ped actorPed, Vector3 location);
      virtual void executeNativesForRecording(Actor actor);
      virtual bool isRecordingItemCompleted(Actor actor, Vector3 location);
      virtual std::string toString();
  };
  
  class ActorVehicleEnterRecordingItem : public ActorVehicleRecordingItem {
  protected:
      int m_vehicleSeat;
      float m_enterVehicleSpeed;
  public:
      ActorVehicleEnterRecordingItem(DWORD ticksStart, Ped actor, Vector3 location, Vehicle veh, int vehicleSeat,float enterVehicleSpeed);
      std::string toString() override;
      void executeNativesForRecording(Actor actor) override;
      bool isRecordingItemCompleted(Actor actor, Vector3 location) override;
  };
  </code></pre>
  
  <p>The vector with the objects which needs polymorphism is stored in another class Actor. It's currently defined as </p>
  
  <pre><code>class Actor
  {
  private:
      std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; m_actorRecordingItems;
  public:
      Actor();
      Actor(Ped ped);
      void setRecording(std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; actorRecordingItems);
      std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; getRecording();
      std::reference_wrapper&lt;ActorRecordingItem&gt; getRecordingAt(int index);
  };
  </code></pre>
  
  <p>With implementation of the key methods as</p>
  
  <pre><code>void Actor::setRecording(std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; actorRecordingItems)
  {
      m_actorRecordingItems = actorRecordingItems;
  }
  
  std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; Actor::getRecording()
  {
      return m_actorRecordingItems;
  }
  
  std::reference_wrapper&lt;ActorRecordingItem&gt; Actor::getRecordingAt(int index)
  {
      return m_actorRecordingItems[index];
  }
  </code></pre>
  
  <p>The actorRecordingItems are created in a separate method and assigned to the actor. This is done through </p>
  
  <pre><code>std::vector&lt;std::reference_wrapper&lt;ActorRecordingItem&gt;&gt; actorRecording;
  actorRecording.reserve(1000);
  
  ActorVehicleEnterRecordingItem recordingItem(ticksSinceStart, actorPed, actorLocation, actorVeh, seat, enterSpeed );
  actorRecording.push_back(recordingItem);
  
  actor.setRecording(actorRecording);
  </code></pre>
  
  <p>Lastly is the location where I want to use them and require the polymorphism.</p>
  
  <pre><code>std::reference_wrapper&lt;ActorRecordingItem&gt; recordingItem = actor.getRecordingAt(recordingPlayback.getRecordedItemIndex());
  
  recordingItem.get().executeNativesForRecording(actor);
  </code></pre>
  
  <p>The last line appears to cause the program to crash. I'm really a bit lost on where to start looking into this problem, so any assistance would be much appreciated</p>
  
  <p>PS sorry for the very long question :)</p>