title: Suppressed the virtual mechanism by storing object explicitly as base class?
body: <p>As you'd expect, I've arrived at a question I can't answer and I can only guess.</p>
  
  <p>Runtime polymorphism is the goal, using the virtual mechanism, but the result I'm getting is as if I called the method while suppressing it; like I called the base class method.</p>
  
  <p>I can only guess then that I am somehow doing that, the call is from an object that <em>is</em> a base class, although it was constructed as a derived class. So, constructed as a derived, stored as a base.</p>
  
  <p>I do store this as a static base class variable within a cpp and interfaced with multiple extern functions to access elsewhere. (perhaps, that's the gotcha?)</p>
  
  <p>GameScene.h:</p>
  
  <pre><code>class GameScene {
  public:
      GameScene() { SceneInit(); }
      ~GameScene() { }
  
      virtual void LevelInit() { } // gcc complains w/o {}
      void SceneInit();
  
      virtual void UpdateLevel( const float&amp; timeDelta ) { } // gcc complains unless {} added
      void UpdateScene( const float&amp; timeDelta );
  };
  
  extern void NewScene( const GameScene&amp; level );
  extern void UpdateScene( const float&amp; timeDelta );
  
  class TestLevel : public GameScene {
  public:
      TestLevel() { SceneInit(); }
      // implementation here in header
      void LevelInit() override
      {
          // level-specific initialization that is apparent at runtime
      }
      void UpdateLevel( const float&amp; timeDelta ) override
      {
          // level-specific checks and performance
          // but, to test, I simply log "This is the test level"
      }
  };
  
  class TutorialLevel : public GameScene {
  public:
      TutorialLevel() { SceneInit(); }
      // implementation here in header
      void LevelInit() override
      {
          // level-specific initialization that is apparent at runtime
      }
      void UpdateLevel( const float&amp; timeDelta )
      {
          // level-specific checks and performance
          // debug log "This is the tutorial level"
      }
  };
  </code></pre>
  
  <p>GameScene.cpp:</p>
  
  <pre><code>#include "GameScene.h"
  
  static GameScene currentScene; // I am now wondering if this pattern is the problem (by explicitly storing this as the base class)
  
  extern void NewScene( const GameScene&amp; level )
  {
      currentScene = level;
  }
  extern void UpdateScene( const float&amp; timeDelta )
  {
      currentScene.UpdateScene( timeDelta );
  }
  
  GameScene::SceneInit()
  {
      // general init
      LevelInit(); // this _properly_ calls the subclass overridden version
      // init completion
  }
  GameScene::UpdateScene( const float&amp; timeDelta )
  {
      // general updating and game handling
  
      UpdateLevel( timeDelta ); // this was _meant_ to call the overridden version, it does not
  }
  </code></pre>
  
  <p>EntryPoint.cpp:</p>
  
  <pre><code>#include "GameScene.h"
  
  int main()
  {
      //NewScene( TestLevel() );
      NewScene( TutorialLevel() );
      float deltaTime;
      while (gameLoop)
      {
          deltaTime = SecondsSinceLastFrame(); // pseudo
          UpdateScene( deltaTime );
      }
  }
  </code></pre>
  
  <p>So, I was following a pattern that worked with SceneInit() calling LevelInit(), which is overridden in the derived classes. If I use either derived class constructor in NewScene(), I get those LevelInit() results at runtime. I thought this would be safe to use this pattern with UpdateScene().</p>
  
  <p>What I see is UpdateScene() called the GameScene::UpdateLevel(), even though it is clearly overridden in the subclasses, just like LevelInit() is.</p>
  
  <p>My (wild) guess is, I <em>am</em> calling UpdateLevel() as if I had explicitly cast it as GameScene. :\</p>
  
  <p>And maybe, this is because I store currentScene <em>as</em> GameScene? (but then, doesn't that defeat the purpose of having polymorphism?)</p>
  
  <p>I am missing something about either storing currentScene, or calling UpdateLevel(). I've tried calling like this:</p>
  
  <pre><code>    GameScene *s = currentScene;
      s-&gt;UpdateLevel();
  </code></pre>
  
  <p>after reading that, as a pointer, the virtual mechanism should be finding the most-derived version of the method. (but alas...)</p>
  
  <p>I wish I could have found an example here or elsewhere, but my searches pointed out problems with virtual in constructors/deconstructors, or just not using the 'virtual' keyword, etc.</p>