title: C++ polymorphism access derived class fields from base class
body: <p>I have the following program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class Base {};
  
  class Deriv : public Base
  {
      public:
          int data;
          Deriv(int data): data(data) {} 
  };
  
  int main()
  {
      Base *t = new Deriv(2);
      std::cout &lt;&lt; t-&gt;data &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>When I compile it, I'm getting the error:</p>
  
  <pre><code>x.cpp: In function ‘int main()’:
  x.cpp:15:21: error: ‘class Base’ has no member named ‘data’
  </code></pre>
  
  <p><strong>How can I get access to data field (note that I don't want to use Deriv *t = new Deriv(2))?</strong></p>