Shallow copy and deep copy implementation

Source: Internet
Author: User
The image assignment in T is the same as in Java, and is passed as a reference. That is, when assigning a pair of values to a variable, the variable is still pointing to the address of the original image. How do you do that? The answer is cloning.

What is clone?

In the actual programming process, we often encounter this situation: there is an object A, in a moment a has already included some valid values, you may need one and a exactly the same new object B, and any subsequent changes to B will not affect the value of a, that is, A and B are two separate objects, But the initial value of B is determined by the A object. In the Java/javasript language, this requirement is not satisfied with simple assignment statements. There are many ways to meet this requirement, but implementing the Clone () method is one of the simplest and most efficient means, and of course there is no such method in the T language.

So I specifically wrote two cloning methods: one for shallow copy, one for deep copy. Explanation:

Shallow copy (Shadow clone): Only the base type of the object is copied, the object type, and still belongs to the original reference.

Deep copy (Deep clone): The basic class of the object is not tightly copied, and the objects in the original object are copied as well. That is, it is entirely new. The following is the test code: Run the Code box [Ctrl + A ALL SELECT hint: You can modify part of the code, and then run] Welcome to our forum discussion!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.