0

对于存储在向量中的对象,我需要在 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很抱歉这个很长的问题:)

4

1 回答 1

1

问题出在以下几行:

ActorVehicleEnterRecordingItem recordingItem(ticksSinceStart, actorPed, actorLocation, actorVeh, seat, enterSpeed );
actorRecording.push_back(recordingItem);

这里发生的是您创建一个局部变量并将对局部变量的引用推送到向量。一旦变量recordingItem超出范围,引用就会失效。std::reference_wrapper正是它的名字所说的——它是一个引用的包装器,它不做任何类型的资源管理。您可以通过使用 的向量来规避这个问题std::unique_ptrstd::shared_ptr或者如果您想避免对每个元素进行动态分配 - boost::variant<>(在这种情况下,您需要指定向量中可能存在的所有潜在类型)。

于 2015-10-13T19:02:39.250 回答