Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating an interface without virtual functions?
    primarykey
    data
    text
    <p>I'm coding a game engine and I have this class set up for objects:</p> <pre><code>class SceneManager //controls everything in the "world" game { public: void Add(SceneObject* object); //adds to the vector private: vector&lt;SceneObject*&gt; _worldObjects; //the vector that contains all of them } </code></pre> <p>And all classes I work on the game inherit from SceneObject:</p> <pre><code>class SceneObject { public: virtual void Draw() = 0; } class Image : public SceneObject { } class Sprite : public SceneObject { } class Model3D : public SceneObject { } </code></pre> <p>So I know I can call Draw() for all objects in my vector. But I've been working on optimizations and I'm trying to get rid of all inheritance and virtual functions, and use composition instead, since they can't be inlined and seems to be a major performance issue when performed on a per-object basis.</p> <p>I'm looking for some C++ technique that I can use to be able to store a bunch of SceneObjects in my vector, and then call Draw() on it and it properly draws the object related to it. This will also work for the Update() function I'm using as virtual.</p> <p>So this code:</p> <pre><code>void SceneManager::Add(SceneObject* object) { _worldObjects.push_back(object); } void SceneManager::DrawTheWorld() { for(unsigned int i = 0; i &lt; _worldObjects.size(); i++) { _worldObjects[i]-&gt;Draw(); //SceneObject's being called } } </code></pre> <p>...would become:</p> <pre><code>void SceneManager::Add(Image* image) { SceneObject* object = new SceneObject(); //link object to image somehow, tried to use it as a member of image _worldObjects.push_back(object); } void SceneManager::DrawTheWorld() { for(unsigned int i = 0; i &lt; _worldObjects.size(); i++) { //_worldObjects[i]-&gt; //I need somehow to be able to get the pointer back to the original class //It can be an image, sprite, model3d, anything } } </code></pre> <p>I don't think if I add a switch or if/elses and removing the virtual I'd gain any performance, so I'm trying to figure if there's a clean way to deal with this.</p> <p>Any ideas?</p>
    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.
 

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