对象的价值不会改变

时间:2015-08-23 15:29:19

标签: c++ reference

我创建了一个应该更改shop对象中的值的方法。不幸的是,这些值没有改变。

编辑:18:13

我可以创建一个新的商店对象并将其返回,但我认为应该通过引用传递对象?

让我的问题更清楚:我唯一的问题是,新值不存储在对象中。我确实运行了调试,并且所有值都正确计算并且符合预期。

问题在于:

shop.get_stock().push_back(inventory_bonbon);

如果此库存项目当前没有库存,此行应将新库存项目推送到向量(包含库存项目)。

此处,当物料当前有货时,我会增加库存物品的数量:

i_inventory_shop.increase_amount(shop.get_stock()[i], amount);

(我已对unit_amount()方法进行了单元测试,并且工作正常。)

这两行是按照预期调用的(意思是我找到一个项目是否有货)。

void IRooms::increase_inventory_shop(Shop & shop, Bonbon & bonbon, int amount)
{
    OutputDebugString("I_Game Logic increase_inventory_shop called \n");

    IInventoryItemsBonbons i_inventory_shop;
    bool bonbon_in_shop = false;

    for (int i = 0; i < shop.get_stock().size(); i++)
    {
        OutputDebugString(("I_Game Logic shop vector size \n" + std::to_string(shop.get_stock().size()) + "\n").c_str());
        OutputDebugString(("I_Game Logic bonbon name \n" + bonbon.get_name() + "\n").c_str());
        OutputDebugString(("I_Game Logic bonbon amount \n" + std::to_string(amount) + "\n").c_str());

        if (bonbon.get_name() == shop.get_stock()[i].get_bonbon().get_name())
        {
            bonbon_in_shop = true;
            OutputDebugString("Bonbon found \n");
            i_inventory_shop.increase_amount(shop.get_stock()[i], amount);
            break;
        }
    }

    if (bonbon_in_shop == false) {
        OutputDebugString("Bonbon not found \n");
        InventoryItemBonbons inventory_bonbon = i_inventory_shop.create(amount, bonbon);
        shop.get_stock().push_back(inventory_bonbon);
    }
}

此方法调用:(以下方法,我已对其进行了测试)

void IInventoryItemsBonbons::increase_amount(InventoryItemBonbons & inventoryitem_shop, int amount)
{
    int old_amount = inventoryitem_shop.get_amount();
    int new_amount = old_amount + amount;
    inventoryitem_shop.set_amount(new_amount);
}

编辑17:51:

Shop.h

std::vector<InventoryItemBonbons> get_stock();

Shop.ccp

std::vector<InventoryItemBonbons> Shop::get_stock()
{
    return stock_bonbons;
}

_____________________________________________________________________________编辑:19:54

我现在介绍了本地变量,然后我返回当地的商店。

Shop IRooms::increase_inventory_shop(Shop & shop, Bonbon & bonbon, int amount)
{
    Shop shop_temp = shop; 
    std::vector<InventoryItemBonbons> inventory_items_temp = shop.get_stock();
    IInventoryItemsBonbons i_inventory_shop;
    bool bonbon_in_shop = false;

    for (int i = 0; i < shop_temp.get_stock().size(); i++)
    {
        if (bonbon.get_name() == shop_temp.get_stock()[i].get_bonbon().get_name())
        {
            bonbon_in_shop = true;
            i_inventory_shop.increase_amount(inventory_items_temp[i], amount);
            break;
        }
    }

    if (bonbon_in_shop == false) {
        InventoryItemBonbons inventory_bonbon = i_inventory_shop.create(amount, bonbon);
        inventory_items_temp.push_back(inventory_bonbon);
    }

    shop_temp.set_stock(inventory_items_temp);

    //shop = shop_temp; 
    //return shop;
    return shop_temp;
}

我唯一想知道的,为什么店铺的价值不会发生变化。我试图将shop_temp复制到商店,但即使这样也行不通。

1 个答案:

答案 0 :(得分:1)

std::vector<InventoryItemBonbons> get_stock();

由于get_stock按值返回,而不是按引用返回,因此一旦临时值超出范围,对返回值的任何更改都将丢失。

shop.get_stock().push_back(inventory_bonbon);

所以这会修改get_stock返回的临时值,它会立即超出范围,被破坏,修改就会丢失。

你可能想要:

std::vector<InventoryItemBonbons>& get_stock();
 ...
std::vector<InventoryItemBonbons>& Shop::get_stock()
{
    return stock_bonbons;
}
相关问题