title: Passing derived class to base function
body: <p>I'm having trouble passing a derived class to a function which accepts the base class as argument. The base class is consists of "obstacles" which are to be placed on a "board"  void Board::setvalue(int length, int width, Obstacle&amp; obstacle);</p>
  
  <p>However, this causes the compiler to give the "no known conversion for argument..."-error. Reading up around the site i found that i should be passing the derived object as a const, this however causes problems because a const can't be assigned to the board (since it holds pointers to non-const Obstacles).<br>
  In turn, changing Board to hold const Obstacles causes a lot of issues elsewhere in the project, especially with the operator&lt;&lt; of Board and Obstacle.<br>
  I have tried passing the objects as consts and then using Obstacle ob = new obstacle(<em>the const obstacle</em>) but this made them generic Obstacle objects rather than Player/Barrel/Wall objects.</p>
  
  <p>Is there any way  to pass these objects as non-consts or assigning them as non-consts? i tried using const_cast() but this caused undefined behaviour.</p>
  
  <p>An example of the function call:</p>
  
  <pre><code>Board_-&gt;setvalue(x, y, Player(data, moveable, x, y));
  </code></pre>
  
  <p>Here is my code:</p>
  
  <p>The base class</p>
  
  <pre><code>class Obstacle
  {
      public:
      Obstacle* _properlyinitialized;
      string Name;
      bool Moveable;
      int x;
      int y;
      Obstacle();
      Obstacle(string Name, bool Moveable, int x, int y);
      virtual ~Obstacle();
      bool properlyInitialized();
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Obstacle&amp; Obstacle);
  };
  </code></pre>
  
  <p>An example of the derived classes (other derived classes don't have special functions yet)</p>
  
  <pre><code>class Player: public Obstacle
  {
  public:
      Player():Obstacle(){};
      Player(string Name, bool Moveable, int x, int y):Obstacle(Name, Moveable, x, y){this-&gt;_properlyinitialized = this;};
      ~Player(){};
      /*void Moveleft();
      void Moveright();
      void Moveup();
      void Movedown();*/
  };
  </code></pre>
  
  <p>The Board class header</p>
  
  <pre><code>class Board
  {
  private:
      Board* _properlyinitialized;
      int length;
      int width;
      Obstacle * * * playfield;
  
  public:
      /*
       **ENSURE(this-&gt;properlyInitialized(),
                  "Object wasn't initialized when calling object");
       */
      Board();
      Board(int length, int width);
      ~Board();
      bool properlyInitialized();
      /*
       **REQUIRE(this-&gt;properlyInitialized(),
              "Object wasn't initialized when calling properlyinitialized");
       */
      void clear();
      const int getLength();
      const int getWidth();
      Obstacle*** getBoard();
      Obstacle* getTile(int length, int width);
      void setvalue(int length, int width, Obstacle&amp; obstacle);
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Board&amp; Board);
  };
  
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stream, Board&amp; Board);
  </code></pre>
  
  <p>And finally, the setvalue function.</p>
  
  <pre><code>void Board::setvalue(int length, int width, Obstacle&amp; obstacle)
  {
      this-&gt;playfield[length][width] = &amp;obstacle;//value;
      return;
  }
  </code></pre>
  
  <p>I'm happy to provide more code if needed.</p>