title: dynamic cast fails on downcasting from parent to child
body: <p>I have the following parent child simple classes:</p>
  
  <pre><code>class BoundBases{
      public:
              virtual ~BoundBases() { }
  };
  
  
  // Rectangular Bounds for tree
  class RectBounds : public BoundBases{
  
      public:
  
              // x, y center point   
  
              double x, y;         
              double w, h;
  
              ~RectBounds(){ }
  
  
              // (_x, _y): center of rectangle bound. (_w, _h): width and height 
              RectBounds(double _x, double _y, double _w, double _h){
                  x = _x;
                  y = _y;
                  w = _w;
                  h = _h;
              }
  
              //... more functions 
  
  };
  </code></pre>
  
  <p>I also have the following function structure:</p>
  
  <pre><code>void MyClass::init( BoundBases &amp;bounds, std::vector&lt;int&gt; &amp;colsPartitioned)
  {
      printf("init - new\n");
      BoundBases * bPtr = &amp;bounds;
      RectBounds * rBounds = dynamic_cast&lt;RectBounds *&gt;(bPtr);
      if(rBounds){
            // do something
  
      }else{
  
              throw runtime_error("dynamic cast fail");
      }
  }
  </code></pre>
  
  <p>The dynamic cast is failing even though I call the function with RectBounds type as an argument. What is the reason?</p>
  
  <p>FIXED:</p>
  
  <p>The function calling init passed BoundBases by value, as follows:</p>
  
  <pre><code> MyClass2::MyClass2( BoundBases boundBases, std::vector&lt;int&gt; colsPartitioned) { // creates new table
  
          // set up partition
          partScheme_ -&gt; setColsPartitioned(colsPartitioned);
          partScheme_ -&gt; setBoundBases(boundBases);
          partScheme_ -&gt; init(boundBases, colsPartitioned);
  
      }
  </code></pre>
  
  <p>I changed the signature to pass by reference and it worked. (&amp;boundBases). Can someone explain why is that? I am new to C/C++. </p>