Recommendation 14: Properly implement shallow and deep copiesThe technique of creating a copy of an object is called a copy (also called a clone). We divide the copy into shallow copy and deep copy.
Shallow copy copies all the fields in the object to the new object (copy). Where the value of the value Type field is copied to the copy, the modifications i
Basic concepts:
Shallow copy: Refers to the object's field is copied, but the field reference object is not copied, the Copy object and the original object only is the reference name is different, but they share one entity. Changes to any one object affect the other. Most of the reference types implement a shallow copy, and the assignment between reference type objects is to copy a copy of an object refere
Weekend free to see the prototype model, which talked about shallow copy and deep copy, think of PHP in the corresponding assignment, cloning and cloning is a shallow copy or deep replication.
Let's take a look at the assignment, such as a resume class, with two attributes of height and weight:
Class Resume
{public
$height;
public $weight;
Public
$workExperience;
}
$ResumeA = new Resume ();
$ResumeB
The default copy constructor for C + + is a shallow copyA shallow copy is a simple assignment between the data members of an object, such as when you design a copy constructor that does not have a class and does not provide it, when you use an object of that class to assign a value to an object, the process that is performed is a shallow copySuch as:Class A {publ
A number, a string, whether it is a shallow copy or a deep copy, is pointing to an address.A = 1"ABC"print (ID (a))print== b Print == copy.copy (b)print== copy.deepcopy (b) print (ID (A3), ID (b3))View CodeSecond, the list, the tuple, the dictionary's shallow copy, the deep copy points to the address is the change.1. Tuples contain lists, dictionaries#define a function: Print the address of all the data
For the normal type, the copy is no big deal.
int a = 0;int B = A;
No problems will occur.
And the class object is different from ordinary object, the inner structure of class object is generally more complex, there are various member variables.
Shallow copy
First of all, we often encounter the case of shallow copies.
#include
This code appears to be fine at first glance, by copying A to B through the clas
Recently read some books, deep copy and shallow copy of the words, always appear in front of the eyes, and then organized a bit, about the principle and implementation of the depth of Java.1. What is a deep/shallow copy?Shallow copy: Copy of the base data type value of the target object and the address of the reference type;Deep copy: A copy of the base data type
First, what is it?Shallow copy : Copy the value of a member variable of a value type , copy only the reference to a member variable of the reference type, and do not copy the referenced object deep copy: a copy of a value for a member variable of a value type , and a reference object for a member variable of a reference type internal mechanism: (1) clone method for Object class The default implementation is
1. Shallow copy vs deep copyThe copy in this article can also be referred to as a copy, which in this article is considered to be the same meaning of copy and copy. In addition, this article only discusses the replication of complex data types in JS (Object,array, etc.), does not discuss the basic data types (Null,undefined,string,number and Boolean), these types of values themselves are stored in the stack memory ( The actual value of the string type
JAVA Shallow heap Retained heap, javashallowheap
I recently studied the memory leakage problem and found Shallow heap Retained heap in the MAT tool.
Then I found some information on the Internet.
Shallow SizeThe memory size occupied by the object itself, excluding the objects referenced by the object.For non-array objects, its size is the sum of the size of th
C + + Primer Learning Notes _20_ class and Data Abstraction (6) _ Deep copy and shallow copy, empty class and empty array One, deep copy and shallow copyShallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, a shallow copy simply duplic
Original URL: http://canofy.iteye.com/blog/258790Java deep copy and shallow copy
Shallow copy and deep copy
//Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object.
//In other words, shallow copy si
process of assigning variables. For complex data structures, the assignment is equal to the full sharing of the resource, and a change in value is completely shared by another value.Sometimes, however, we need to keep a copy of the original content of a piece of data, and then process the data, and it's not wise to use assignments at this time. Python provides a copy module for this requirement. Two main copy methods are available, one for normal copy and the other for deepcopy. We call the for
Overview:in. NET, the ability to copy objects is provided, provided the ICloneable interface is implemented. There is a clone method in the ICloneable interface,you can override the implementation of a custom copy method in a class. There are two ways to implement object replication: deep copy and shallow copy. Deep copy and shallow copy:shallow copy, shallow cop
The main factors that determine depth of fields are aperture size, length of lens focal length and distance of the object being photographed. Shallow depth of the subject can be separated from the background (sometimes the foreground), so that only the main body of the focus and the background is virtual. The use of this technique visually stretched the distance between the subject and the surrounding background, making the subject more prominent in t
One, Assignment (Assignment)>>> a = [1, 2, 3]>>> B = a>>> print (ID (a), id (b), sep= ' \ n ') 139701469405552139701469405552 In Python, assigning a variable to another variable is actually adding a "label" to the object currently in memory.As in the example above, by using the built-in function ID (), you can see that a and B point to the same object in memory. a is bwill return True.Second, shallow copy (shallo
Like many languages, Python is divided into simple assignments, shallow copies, and deep copies of these "copy" methods.
In the learning process, the initial understanding of the shallow copy is very vague. But after a series of experiments, I found that the concepts of these three are further understood.
First, assign a value
The assignment is the most common of the three operations, and we analyze the as
. When the value of str1= "ABC" Changes, the memory is checked, the string is not present, the memory space is redistributed, the "ABC" is stored, and the address is assigned to STR1, and str2 still points to the address of "AB". The results in Example 1 can be verified.Conclusion:String is a reference type, except that the compiler has handled it specially.About the discussion: http://social.msdn.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/ce580186-86d9-45f7-b5ff-20302caf1324About C # d
Introduction to Python's shallow copy, deep copy, and reference mechanism, and introduction to python's Reference Mechanism
I encountered some problems this week and realized that if the basic knowledge has not been consolidated for a period of time, I still need to review it. Here I will take a note and record it.
Prefix
First, briefly describe the problem encountered. The requirement is to write two print results.
As you can see, a points to a list
can be used for the following scenarios:
Class initialization needs to digest a lot of resources, including data, hardware resources, etc.
A prototype pattern can be used to produce an object through new that requires very tedious data preparation or access rights;
When an object needs to be provided to other object access, and each caller may need to modify its value, consider using the prototype schema to copy multiple objects for use by the caller;
Whe
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.