tags:

views:

75

answers:

0

I have a class InventoryScreen that has a reference to another class GameData as a member, like this:

class InventoryScreen {
    private:
        GameData& gameData;

    public:
        InventoryScreen(GameData& gd): gameData(gd) {}
    (...)
};

class GameData {
    private:
        std::vector<Item> items;
    (...)
};

In a member function of GameData, it passes a reference to itself to a function that creates an InventoryScreen:

void GameData::displayInventory() {
    otherObject.anotherFunction(*this);
}

void OtherObject::anotherFunction(GameData& gd) {
    InventoryScreen screen(gd);
}

screen adds an element to the items vector of its GameData reference, and inside screen the new element is there. But if I check GameData directly, instead of through the reference, items has the same number of elements as before. This direct check happens while screen still exists.

Anyone see what I'm doing wrong? It seems like screen has a copy of GameData instead of a reference to it.