Here I have tried to mimic clone method of uvm_object.
typedef base_class;
typedef derived_class;
function base_class create();
derived_class tmp;
tmp = new();
return tmp;
endfunction
class base_class;
virtual function do_copy(base_class rhs);
endfunction
virtual function copy(base_class rhs);
do_copy(rhs);
endfunction
virtual function base_class clone();
base_class base;
base = create();
base.copy(this);
return (base);
endfunction
endclass
class derived_class extends base_class;
int A = 5;
function do_copy(base_class rhs);
derived_class derived;
$cast(derived,rhs);
super.do_copy(rhs);
A = derived.A;
endfunction
function display();
$display("A is %0d",A);
endfunction
endclass
module test();
derived_class d1,d2;
initial
begin
d1 = new();
d1.A = 10;
$cast(d2,d1.clone());
d1.display();
d2.display();
end
endmodule
Output :
A is 10
A is 10
Here at base.copy(this);
“base” variable is of type base_class, how come it access variable “A” which is defined in derived_class?
Correct me if I am wrong somewhere.