|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



COPY


The terms "deep copy" and "shallow copy" refer to the way objects are copied, for example, during the invocation of a copy constructor or assignment operator.

EXAMPLE:
class B;
int i;
endclass

program main;
initial
begin
B b1;
B b2;
b1 = new();
b1.i = 123;
b2 = b1;
$display( b2.i );
end
endprogram
RESULTS:

123




In the above example, both objects are pointing to same memory. The properties did not get copied. Only the handle is copied.


Shallow Copy



A shallow copy of an object copies all of the member field values.


EXAMPLE:
class B;
int i;
endclass

program main;
initial
begin
B b1;
B b2;
b1 = new();
b1.i = 123;
b2 = new b1;
b2.i = 321;
$display( b1.i );
$display( b2.i );
end
endprogram

RESULTS:

123
321




This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The assignment operator make shallow copies.



EXAMPLE:
class A;
int i;
endclass

class B;
A a;
endclass

program main;
initial
begin
B b1;
B b2;
b1 = new();
b1.a = new();
b1.a.i = 123;
b2 = new b1;
$display( b1.a.i );
$display( b2.a.i );
b1.a.i = 321;
$display( b1.a.i );
$display( b2.a.i );

end
endprogram
RESULT

123
123
321
321




In the above example, the varible i is changed to which is inside the object of . This changes in seen in also because both the objects are pointing to same memory location.


Deep Copy



A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences.



EXAMPLE:
class A;
int i;
endclass

class B;
A a;

task copy(A a);
this.a = new a;
endtask

endclass

program main;
initial
begin
B b1;
B b2;
b1 = new();
b1.a = new();
b1.a.i = 123;
b2 = new b1;
b2.copy(b1.a);
$display( b1.a.i );
$display( b2.a.i );
b1.a.i = 321;
$display( b1.a.i );
$display( b2.a.i );

end
endprogram

RESULTS:

123
123
321
123


Clone



A clone method returns a new object whose initial state is a copy of the current state of the object on which clone was invoked. Subsequent changes to the clone will not affect the state of the original. Copying is usually performed by a clone() method method of a class which is user defined. This method usually, in turn, calls the clone() method of its parent class to obtain a copy, and then does any custom copying procedures. Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). After obtaining a copy from the parent class, a class's own clone() method may then provide custom cloning capability, like deep copying (i.e. duplicate some of the structures referred to by the object) .

One disadvantage is that the return type of clone() is Object, and needs to be explicitly cast back into the appropriate type (technically a custom clone() method could return another type of object; but that is generally inadvisable).

One advantage of using clone() is that since it is an overridable method, we can call clone() on any object, and it will use the clone() method of its actual class, without the calling code needing to know what that class is (which would be necessary with a copy constructor).



EXAMPLE:
????
Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer