Shallow copy causing crashes

I have a custom created class which I'm using to modify a custom data type using overloaded operators.

1
2
3
4
operator +(/* arguments */);
operator -(/* arguments */);
operator *(/* arguments */);
//etc. 


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.)

Thanks for any help on this issue.
Last edited on
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?
Who writes a C++ class with pointers and doesn't add a copy constructor or has the copy constructor make a shallow copy, anyway?


AMD. :)

I don't think they intended for the data type I'm using to be used with pointers.
Last edited on
Topic archived. No new replies allowed.