mercredi 1 juillet 2015

C++11 for loop through vector of unique_ptr

Having trouble correctly looping through a vector of unique_ptrs to my own custom object. I've provided pseudo-code below which isn't fully fleshed out, but focus on the for loop. I'd like to use the C++11 "for" loop, and iterate over the vectors - OR from what I've heard, providing your own iterators is better? I just don't know how to do this when I have separate classes. If I'm keeping the vector in a manager class, then where should I define the iterator methods? In the object class, or the manager class? I also want to make sure that my data stays const, so the actual values aren't able to be changed.

// Class for our data
Class GeoSourceFile
{
    // some data, doesn't matter
    double m_dNumber;
    int    m_nMyInt;
}
// singleton manager class
Class GsfManager
{
  public:
    // Gets pointer to the vector of pointers for the GeoSourceFile objects
    const std::vector<std::unique_ptr<GeoSourceFile>>* GetFiles( );
  private:
    // Vector of smart pointers to 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 )
{
    //...
}

Aucun commentaire:

Enregistrer un commentaire