I have always understood that a deep copy for a class couldn't be implemented automatically for the reason that no compiler can know exactly how to perform a deep copy for a class. Therefore, the only copy that a compiler can perform automatically on a class is a shallow copy. If you want a deep copy of a class you have to write it yourself.
Interestingly, I have just found a class where I don't think a deep copy can be written. Therefore, the class can't have a copy constructor, or even truly clone itself. That seems a bit baffling to me. Kind of like a paradox: A class can't know enough about itself to create a copy of itself. I'm wondering whether or not I am thinking about this wrong.
The class in question wraps the movement of objects from point A to point B. Therefore, it has such things as the number of items moved, the source (A), the destination (B), and a few other descriptive items about the move. However, I realized that there could be multiple moves from A to B, and that all of these moves needed to be tied together. Therefore, I added a Twins list to the class. Thus, the class looks like this (with much extraneous stuff removed):
I believe that this class cannot clone itself if there is anything in mTwins. The reason is that it can't clone the objects in mTwins, nor can it get them from elsewhere. All objects of class Foo exist in some larger set, with mTwins holding nothing more than a subset of the larger set. If every member of the larger set were to clone itself, then each member would be unable to determine which of the clones would belong in mTwins.
Therefore, there are classes where a Deep Copy is flat out impossible from within the class. Is that right?
Interestingly, I have just found a class where I don't think a deep copy can be written. Therefore, the class can't have a copy constructor, or even truly clone itself. That seems a bit baffling to me. Kind of like a paradox: A class can't know enough about itself to create a copy of itself. I'm wondering whether or not I am thinking about this wrong.
The class in question wraps the movement of objects from point A to point B. Therefore, it has such things as the number of items moved, the source (A), the destination (B), and a few other descriptive items about the move. However, I realized that there could be multiple moves from A to B, and that all of these moves needed to be tied together. Therefore, I added a Twins list to the class. Thus, the class looks like this (with much extraneous stuff removed):
Code:
Class Foo
Private mSource As Guid
Private mDestination AS Guid
Private mTwins As New List(of Foo)
End Class
Therefore, there are classes where a Deep Copy is flat out impossible from within the class. Is that right?