title: Dynamically allocated data members in class constructor
body: <p>I am having issues with trying to figure out how I would turn a data member inside a class (which was originally an int) into a pointer to a dynamically allocated piece of memory.</p>
  
  <p>I know I can do int *num = new int under normal circumstances, but how would I initialize it in a class?</p>
  
  <p>My teacher did an amazing job of not explaining this in the crappiest way possible in class -_-.</p>
  
  <p>This is an example of the class and the constructor.</p>
  
  <p>The header</p>
  
  <pre><code>class Double
  {
  private:
  double *val;
  </code></pre>
  
  <p>The .cpp</p>
  
  <pre><code>Double::Double()
  {
  this-&gt;equals(0.0);
  }
  
  Double::Double(const Double &amp;d)
  {
  this-&gt;equals(d.val);
  }
  
  Double::Double(const double &amp;d)
  {
  this-&gt;equals(d);
  }
  
  Double::Double(const Interger &amp;i)
  {
  this-&gt;equals(i.toInt());
  }
  
  //New overloaded constructor using strings
  Double::Double(const string &amp;s)
  {
  this-&gt;equals(s);
  }
  
  void Double::equals(const double &amp;d)
  {
  this-&gt;val = d;
  }
  </code></pre>
  
  <p>All I know is I have to make the data member a pointer now, but I have no idea how to create the new memory.  I tried looking this up but I could not find an example of how to do DAM inside an actual class for its memory and constructor.</p>
  
  <p><em>EDIT</em>
  Solution was a simpler then I thought.  </p>
  
  <p>Double::Double() : val(new double)
  {
  ......
  }</p>
  
  <p>just have to do that to every constructor, then change any instance of d.val or this->val to *d.val or *this->val.</p>