The class contains a pointer to the custom data type...
dataType* data;
and the pointer is used to refer to a dynamically allocated instance of the data type in the class' constructor.
1 2
//Inside the constructor
data = new dataType(/* arguments */);
My problem is that this custom data type is not my own, and the copy constructor does a shallow copy by default. When I try to assign a value to the data type...
data = *someOtherClass.getDataPtr();
a shallow copy is done. This means that when I attempt to free the memory being pointed to by "data," the application crashes.
1 2
delete data;
//Segfault
Is there any way to force a deep copy on dataType? I have the source to dataType, but I'd rather not modify it if I don't have to. (Modifying the source of dataType would require people wanting to compile the application on their own to have my modified version of the library.)
Is there any way to force a deep copy on dataType?
No, you'll have to either modify the (or add a) copy constructor. Who writes a C++ class with pointers and doesn't add a copy constructor or has the copy constructor make a shallow copy, anyway?