I have a pointer to an object of type A:
A* a;
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).
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:
A* a = param_a;
A temp = *a;
A* b = new A();
*b = temp;
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?