C++11 for loop through vector of unique_ptr -


having trouble correctly looping through vector of unique_ptrs own custom object. i've provided pseudo-code below isn't fleshed out, focus on loop. i'd use c++11 "for" loop, , iterate on vectors - or i've heard, providing own iterators better? don't know how when have separate classes. if i'm keeping vector in manager class, should define iterator methods? in object class, or manager class? want make sure data stays const, actual values aren't able changed.

// class our data class geosourcefile {     // data, doesn't matter     double m_dnumber;     int    m_nmyint; } // singleton manager class class gsfmanager {   public:     // gets pointer vector of pointers geosourcefile objects     const std::vector<std::unique_ptr<geosourcefile>>* getfiles( );   private:     // vector of smart pointers geosourcefile objects     std::vector<std::unique_ptr<geosourcefile>> m_vgeosourcefiles;   } void app::ondrawevent {     gsfmanager* pgsfmgr = gsfmanager::instance();     for(auto const& gsf : *pgsfmgr->getfiles() )     {          oglobj->drawgeosourcefile( file );     } }  void oglclass::drawgeosourcefile( std::unique_ptr<geosourcefile> file ) {     //... } 

found answer issues myself.

important thing remember here cannot create copy of unique_ptr... includes passing pointer other functions. if pass unique_ptr function, must use & character in receiving function.

for example:

void oglclass::drawgeosourcefile( const std::unique_ptr<geosourcefile> file ) {     // fails compile, you're getting copy of pointer, not allowed }  void oglclass::drawgeosourcefile( const std::unique_ptr<geosourcefile>& file ); {     // compiles, you're using original pointer } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -