Virtual (Abstract) vs Concrete
The clone method is used to provide a deep (nested) copy of an object.
clone first allocates new memory for the object, then copies over each field to the new object. If a field is an object handle, then instead of copying the handle (which would do a "shallow" copy) you would call fieldname.clone() to recursively allocate memory for that field (a "deep" copy).
Clone (Virtual) vs Copy (Concrete)
class base;
int p1;
function void copy(base orig);
this.p1 = orig.p1;
endfunction
endclass
class ex_base;
int p2;
function void copy(base orig);
super.copy(b);
this.p2 = orig.p2;
endfunction
endclass
base b1,b2;
ex_base eb1, eb2;
initial begin
eb1 = new; eb2 = new();
eb2.p2 = 5;
b1 = eb1; b2 = eb2;
b1.copy(b2); // p2 is not copied
eb1.copy(eb2); // p2 is copied
end
// Since copy() is not virtual, calling b1.copy() calls base::copy(),
// and the additional property p2 is not copied even though it exists
// in object referenced by b1.
No comments:
Post a Comment