mercredi 29 avril 2015

vector of unique_ptr in C++11

I recently switched to C++11 and I'm trying to get used to good practices there. What I end up dealing with very often is something like:

class Owner
{
private:
    vector<unique_ptr<HeavyResource>> _vectorOfHeavyResources;
public:
    virtual const vector<const HeavyResource*>* GetVectorOfResources() const;
};

This requires me to do something like adding a _returnableVector and translating the source vectors to be able to return it later on:

_returnableVector = vector<HeavyResource*>;
for (int i=0; i< _vectorOfHeavyResources.size(); i++)
{
    _returnableVector.push_back(_vectorOfHeavyResources[i].get());
}

Has anyone noticed similar problem? What are your thoughts and solutions? Am I getting the whole ownership idea right here?

Aucun commentaire:

Enregistrer un commentaire