samedi 29 juin 2019

Transferring a vector

I have a class which contains a vector<shared_ptr<T>>:

using SharedItems = std::vector<std::shared_ptr<Item>>;

class LargeLift
{
public:
    SharedItems& getItems()
    {
        return _items;
    }

    void setSharedItems(SharedItems& items)
    {
        _items = items;
    }

private:
    SharedItems _items;
};

I then do the following:

LargeLift a;
// Gets populated

SharedItems& items = a.getItems();

LargeLift b;
b.setSharedItems(items);

// Assume a now goes out of scope but b is being used

Is it safe to retrieve the vector of shared pointers by reference, to then re-assign by reference, or am I violating the sharing mechanism and I should be making a copy?

Aucun commentaire:

Enregistrer un commentaire