title: Deep copy of a pointer to an array of pointers to objects
body: <p><em>Edit: I originally posed this question out of context so I've reworked it. I've left as much as possible unchanged so most of your responses will still apply.</em> </p>
  
  <p>I'm having trouble understanding how to implement a constructor which accepts a pointer to an array of pointers.</p>
  
  <p>I have the following class which contains a member, <code>bodies</code>, of type <code>Body**</code> (i.e. it is a pointer to an array of pointers to body objects).</p>
  
  <pre><code>class Galaxy
  {
  private:
     int n;          // Number of bodies in galaxy.
     Body** bodies;  // Ptr to arr of ptrs to Body objects.
  
  public:
     Galaxy();
     Galaxy(int, Body**);
  
     // Some other member functions.
  };
  </code></pre>
  
  <p>Here is the implementation of the constructors:</p>
  
  <pre><code>// Default constructor. Initializes bodies to null pointer.
  Galaxy::Galaxy() : bodies(NULL) {}
  
  // Alternate constructor. Here I try to perform a deep copy of bodiesIn.
  Galaxy::Galaxy(int nIn, Body** bodiesIn)
  {
     n = nIn;
  
     // Allocate memory for an array of n pointers to Body objects.
     bodies = new Body*[n];
     // Perform deep copy.
     for (int i=0; i&lt;n; i++)
     {
        bodies[i] = new Body;
        *bodies[i] = *bodiesIn[i];
     }
  }
  </code></pre>
  
  <p>Is this method sound, or is there a preferred way to construct such an object.</p>
  
  <p>P.S. I realize it would be easier to code this with <code>std::vector</code>'s, however the size of the array doesn't change, and minimizing memory usage is important.</p>