Well maybe. It simply is not created implicitly. If I had to guess, this is probably due to the fact that Java objects are always allocated as heaps.
In C ++, the default instance constructor is a small batch copy. If the class owns the memory allocated on the heap (using the raw pointer), this will force the copy to share the originals with the original, which is not what you want.
Imagine that Java has this behavior. Any class that has fields that are objects (read: essentially all of them) will have the wrong behavior, and you will need to override it yourself. In 99% of cases, you have not helped anyone. In addition, you just created a subtle trap for yourself - imagine that you accidentally forgot to override the default copy constructor. If it was created by default and you are trying to use it, the compiler will not complain at all, but your program will behave badly at runtime.
Even if they created a default copy constructor that performs a deep copy, I'm not sure if that would be particularly useful. Not only do you usually make fewer copies in Java than C ++, but you do not always want to copy a field.
The objects that you just own, and the objects that you refer to because you need them but are not responsible for them, are the same - just fields. Property and borrowing are not first class concepts. For the objects that you have, you want to copy them (if they are not immutable, in which case you should not worry), and for the objects to which you simply keep the link, you want to copy the link.
I would say that the copy constructor, which simply thoughtlessly deeply copies everything, is not suitable for many classes. Of course, more than shallow copying by default.
Kevin Aug 29 '16 at 18:26 2016-08-29 18:26
source share