title: C++ “Unable to read memory” when accessing pointer object from inherited class
body: <p>I have this error:</p>
  
  <pre><code>Exception thrown at 0x0108C6E9 in myprojectname.exe: 0xC0000005: Access violation reading location 0x00000028.
  </code></pre>
  
  <p>However, this only happens when I call a function from the base class via the derived class.</p>
  
  <p>I need a pointer to the space object because my base class also needs that. I am not sure if this is needed.</p>
  
  <p>This is how I call the functions of the base(Player) class:</p>
  
  <pre><code>space-&gt;getEnemy().drawPlayer(); //FIRST I GET THE OBJECT OF THE DERIVED(ENEMY CLASS) AND THAN I CALL A FUNCTION FROM THE BASE CLASS(PLAYER CLASSS)
  </code></pre>
  
  <p>The error message "Unable to read memory" happens in getter functions in a class that the base class needs(for example a pointer to the window).</p>
  
  <p>Does anyone know what I am doing wrong?</p>
  
  <p>I initialize that space pointer to my import class named Space, in every class of my project. The program works fine with those space pointers if I don't call the inherited stuff of Enemy class.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I found out that the object to the (very important) Space class, is NULL in the base class. The enemy class space object is not NULL, but the space object of the class it inherits from, is NULL. Does anyone know how an inherited pointer object can be NULL?</p>
  
  <p>A picture to make it clearer:</p>
  
  <p><a href="http://prntscr.com/9btfim" rel="nofollow">http://prntscr.com/9btfim</a></p>
  
  <p><strong>EDIT 2 MY CURRENT CODE:</strong></p>
  
  <p>Enemy.h (THE DERIVED CLASS)</p>
  
  <pre><code>#pragma once
  
  class Space;
  class Enemy : public Player{
  public:
      void updateEnemy();
  
  private:
  
  };
  </code></pre>
  
  <p>Enemy.cpp</p>
  
  <pre><code>#include "Space.h"
  #include "Enemy.h"
  #include "Player.h"
  #include &lt;iostream&gt;
  
  void Enemy::updateEnemy(){
      if (space == nullptr) {
          std::cout &lt;&lt; "null"; //IT PRINTS NULL
      }
  }
  </code></pre>
  
  <p>A part of player.h (THE BASE CLASS)</p>
  
  <pre><code>class Space; //forward declaration
  public:
      void init(Space * s);
  protected:
      Space * space;
  </code></pre>
  
  <p>A part of player.cpp</p>
  
  <pre><code>void Player::init(Space * s){ //INITIALIZING SPACE CLASS OBJECT
      space = s;
  }
  </code></pre>