对于存储在向量中的对象,我需要在 C++ 中使用多态性。
从其他问题中了解,向量中的对象需要通过引用存储,否则会发生对象切片。
我的理解是 std::reference_wrapper 会很合适,但我在实现它时遇到了一些问题。
ActorRecordingItem 是基类,ActorVehicleEnterRecordingItem 是需要多态性的类的示例(通过 ActorVehicleRecordingItem 从 ActorRecordingItem 继承)
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;
};
具有需要多态性的对象的向量存储在另一个类 Actor 中。目前定义为
class Actor
{
private:
std::vector<std::reference_wrapper<ActorRecordingItem>> m_actorRecordingItems;
public:
Actor();
Actor(Ped ped);
void setRecording(std::vector<std::reference_wrapper<ActorRecordingItem>> actorRecordingItems);
std::vector<std::reference_wrapper<ActorRecordingItem>> getRecording();
std::reference_wrapper<ActorRecordingItem> getRecordingAt(int index);
};
随着关键方法的实施
void Actor::setRecording(std::vector<std::reference_wrapper<ActorRecordingItem>> actorRecordingItems)
{
m_actorRecordingItems = actorRecordingItems;
}
std::vector<std::reference_wrapper<ActorRecordingItem>> Actor::getRecording()
{
return m_actorRecordingItems;
}
std::reference_wrapper<ActorRecordingItem> Actor::getRecordingAt(int index)
{
return m_actorRecordingItems[index];
}
actorRecordingItems 在单独的方法中创建并分配给actor。这是通过
std::vector<std::reference_wrapper<ActorRecordingItem>> actorRecording;
actorRecording.reserve(1000);
ActorVehicleEnterRecordingItem recordingItem(ticksSinceStart, actorPed, actorLocation, actorVeh, seat, enterSpeed );
actorRecording.push_back(recordingItem);
actor.setRecording(actorRecording);
最后是我想使用它们并需要多态性的位置。
std::reference_wrapper<ActorRecordingItem> recordingItem = actor.getRecordingAt(recordingPlayback.getRecordedItemIndex());
recordingItem.get().executeNativesForRecording(actor);
最后一行似乎导致程序崩溃。我真的有点迷失从哪里开始研究这个问题,所以任何帮助将不胜感激
PS很抱歉这个很长的问题:)