Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use free functions to model the <code>drawable</code> aspect of your objects:</p> <pre><code>#include &lt;iostream&gt; class Image { }; class Sprite { }; class Model3D { }; namespace draw_aspect { void draw(Image const&amp; image) { std::cout &lt;&lt; "drawing image\n"; } void draw(Sprite const&amp; sprite) { std::cout &lt;&lt; "drawing sprite\n"; } void draw(Model3D const&amp; model3D) { std::cout &lt;&lt; "drawing model3D\n"; } } </code></pre> <hr> <p>Now, either use three separate vectors (this could well be most optimal, depending on the ordering relationship between the objects across collections?), or consider a variant type vector:</p> <h2>1. Using variant types</h2> <pre><code>#include &lt;boost/variant.hpp&gt; using SceneObject = boost::variant&lt;Image, Sprite, Model3D&gt;; namespace draw_aspect { struct draw_visitor : boost::static_visitor&lt;&gt; { template &lt;typename T&gt; void operator()(T const&amp; t) const { draw(t); } }; void draw(SceneObject const&amp; sobj) { static const draw_visitor _vis; boost::apply_visitor(_vis, sobj); } } </code></pre> <p>A complete proof of concept of the latter: <strong><a href="http://coliru.stacked-crooked.com/a/dfced2cee5b4d4b0" rel="noreferrer">Live on Coliru</a></strong></p> <pre><code>#include &lt;vector&gt; class SceneManager //controls everything in the "world" game { public: void Add(SceneObject v) { _worldObjects.emplace_back(std::move(v)); } friend void draw(SceneManager const&amp; sm) { return sm.draw(); } private: void draw() const { for(auto&amp; sobj : _worldObjects) draw_aspect::draw(sobj); } std::vector&lt;SceneObject&gt; _worldObjects; //the vector that contains all of them }; int main() { SceneManager sman; sman.Add(Image()); sman.Add(Sprite()); sman.Add(Model3D()); sman.Add(Image()); draw(sman); } </code></pre> <p>Outputs </p> <pre><code>drawing image drawing sprite drawing model3D drawing image </code></pre> <h2>2. Separate collections</h2> <p>The alternative using separate vectors: <strong><a href="http://coliru.stacked-crooked.com/a/09c65e2d5e184c18" rel="noreferrer">Live on Coliru</a></strong></p> <pre><code>class SceneManager //controls everything in the "world" game { public: void Add(Image v) { _images .emplace_back(std::move(v)); } void Add(Sprite v) { _sprites .emplace_back(std::move(v)); } void Add(Model3D v) { _model3Ds.emplace_back(std::move(v)); } friend void draw(SceneManager const&amp; sm) { return sm.draw(); } private: void draw() const { for(auto&amp; sobj : _images) draw_aspect::draw(sobj); for(auto&amp; sobj : _sprites) draw_aspect::draw(sobj); for(auto&amp; sobj : _model3Ds) draw_aspect::draw(sobj); } std::vector&lt;Image&gt; _images; std::vector&lt;Sprite&gt; _sprites; std::vector&lt;Model3D&gt; _model3Ds; }; int main() { SceneManager sman; sman.Add(Image()); sman.Add(Sprite()); sman.Add(Model3D()); sman.Add(Image()); draw(sman); } </code></pre> <p>Note that the output is <strong>different</strong> (ordering):</p> <pre><code>drawing image drawing image drawing sprite drawing model3D </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload