c++ - vector of unique_ptr in C++11 -
i switched c++11 , i'm trying used practices there. end dealing like:
class owner { private:     vector<unique_ptr<heavyresource>> _vectorofheavyresources; public:     virtual const vector<const heavyresource*>* getvectorofresources() const; }; this requires me adding _returnablevector , translating source vectors able return later on:
_returnablevector = vector<heavyresource*>; (int i=0; i< _vectorofheavyresources.size(); i++) {     _returnablevector.push_back(_vectorofheavyresources[i].get()); } has noticed similar problem? thoughts , solutions? getting whole ownership idea right here?
update: heres thing: if 1 class returns result of processing vector<unique_ptr<heavyresource>> (it passes ownership of results caller), , supposed used subsequent processing:
vector<unique_ptr<heavyresource>> partialresult = _processor1.process(); // translation auto result = _processor2.process(translatedpartialresult); // argument of process vector<const heavyresource*> 
i suggest instead of maintaining , returning un-unique_ptred vector, provide functions access elements directly. encapsulates storage of resources; clients don't know stored unique_ptrs, nor kept in vector.
one possibility use boost::indirect_iterator dereference unique_ptr automatically:
using resourceiterator =      boost::indirect_iterator<std::vector<std::unique_ptr<heavyresource>>::iterator,                               const heavyresource>; resourceiterator begin() { return std::begin(_vectorofheavyresources); } resourceiterator end() { return std::end(_vectorofheavyresources); } 
Comments
Post a Comment