title: Copy constructor with pointers
body: <p>I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.</p>
  
  <p>To learn that, I have made the following simple code. It compiles, but gives me runtime error when performing the copy constructor.</p>
  
  <p>I am trying to copy just the value from the pointer of the copied object, but avoiding assigning the same address.</p>
  
  <p>So, what's wrong here?</p>
  
  <pre><code>    class TRY{
          public:
          TRY();
      ~TRY();
          TRY(TRY const &amp;);
  
          int *pointer;
  
          void setPointer(int);
      };
  
  
      void TRY::setPointer(int a){
          *pointer = a;
  
          return;
      }
  
  
      TRY::TRY(){}
  
  
      TRY::~TRY(){}
  
  
      TRY::TRY(TRY const &amp; copyTRY){
          int a = *copyTRY.pointer;
          *pointer = a;
      }
  
  
  
      int main(){
  
          TRY a;
          a.setPointer(5);
  
          TRY b = a;
  
          b.setPointer(8);
  
          cout &lt;&lt; "Address of object a = " &lt;&lt; &amp;a &lt;&lt; endl;
          cout &lt;&lt; "Address of object b = " &lt;&lt; &amp;b &lt;&lt; endl;
  
          cout &lt;&lt; "Address of a.pointer = " &lt;&lt; a.pointer &lt;&lt; endl;
          cout &lt;&lt; "Address of b.pointer = " &lt;&lt; b.pointer &lt;&lt; endl;
  
          cout &lt;&lt; "Value in a.pointer = " &lt;&lt; *a.pointer &lt;&lt; endl;
          cout &lt;&lt; "Value in b.pointer = " &lt;&lt; *b.pointer &lt;&lt; endl;
  
          return 0;
      }
  </code></pre>
  
  <p>I'll be using this concept for other classes with lots of pointers in it, where I need to copy all values from on object to the other. Copying is initially necessary for this code, so I would like to keep the copying possibility (I won't be hiding the copy constructor as private).</p>
  
  <p>Besides, the real class I need to implement has like 10 pointers, and it might be changing with time. Isn't there a somewhat smarter way to have a deep copy constructor in C++?...</p>