DRAWING A SKETCH

All the elements that make up a sketch are safely stored in the document object. You now need a way to display the sketch in the view. The OnDraw() function in CSketcherView will do this. Obviously, it will need access to the data for a sketch that is owned by the document object, and ideally this should happen without the document object exposing it to external modification. One way to achieve this is for the document object to make const iterators available. The iterator type name for a sketch is a little long-winded and you can economize on typing by adding the following type definition to SketcherDoc.h after the #include directives:

typedef std::list<std::shared_ptr<CElement>>::const_iterator SketchIterator;

You can now use SketchIterator in the code to specify the type for a const iterator for the sketch list container. By adding the following functions to the CSketcherDoc class definition you will enable the view object to obtain the iterators it needs to draw a sketch:

  // Provide a begin iterator for the sketch
  SketchIterator begin() const { return std::begin(m_Sketch);  }
 
  // Provide an end iterator for the sketch
  SketchIterator end() const { return std::end(m_Sketch);  }

These should be public, and you can put them in the Operations section of CSketcherDoc. A const iterator can be incremented and dereferenced but it cannot be used to modify what it points to.

You can use these functions to implement the OnDraw() member of CSketcherView:

void CSketcherView::OnDraw(CDC* ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.