title: How to copy C++ pointer data?
body: <p>I have a pointer to an object of type A:</p>
  <p>A* a;</p>
  <p>It is set by a parameter in an initialization function, but I don't have any way of knowing whether a type derived from A is used instead, and I can't test for all possibilities (and shouldn't have to, anyway).</p>
  <p>I need to make an exact copy of the object pointed to by a, and have the new duplicate object pointed to by a new pointer b.  It must copy all of the data, rather than just the pointer itself, because I must NOT overwrite the original data when I later change *b.  I could try this:</p>
  <p>A* a = param_a;</p>
  <p>A temp = *a;</p>
  <p>A* b = new A();</p>
  <p>*b = temp;</p>
  <p>But I'm afraid that when I try to store the data from *a into temp, if it wasn't actually instantiated as an A then there won't be enough space to hold it.  Then when I instantiate *b as an A, and then replace it with temp, again there wouldn't be enough space.  How can I do this?</p>