In reply to David Peng:
You are confusing the concepts of child and parent objects of UVM components with inheritance. It would have been much clearer if you named your classes c1_class for parent_class and c2_class for child_class.
Your error is because you are constructing a UVM parent component c2 (which you called child_class) and trying create 2 child uvm_component class objects with the same string name “slv”. The types of those components are irrelevant. The UVM does not allow 2 child components to have the same string name under the same parent. Another problem you did not get far enough into to encounter is the uvm_field macros do not work correctly when you have fields with the same name in both the base and extended class. And finally you cannot comment out the call to super.new, the compiler will insert it for if you leave it out. Maybe that’s why you thought you were only constructing one “slv” child object.
I believe once you understand what I wrote above you might want to reconsider rethinking your approach to the entire problem. But there is one way you could proceed by making sure only one child object gets constructed using the build_phase instead of constructing components in new().
class c1_class extends uvm_component;
bit [31:0] addr;
pmem_class slv;
`uvm_component_utils(c1_class)
function new(string name = "c1_class", uvm_component parent = null);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
slv = pmem_class::type_id::create("slv", this);
endfunction
virtual function void display();
$display("Addr = %0d",addr);
$display("slv.a = %0d", slv.a);
endfunction
endclass
class c2_class#(type T=cmem_class) extends c1_class;
bit [31:0] data;
T slv; // this is a very bad practice
`uvm_component_param_utils(c2_class)
function new(string name = "child_class", uvm_component parent = null);
super.new(name, parent);
function void build_phase(uvm_phase phase);
// do not call super.build();
slv = T::type_id::create("slv", this);
super.slv = slv; // both member have a handle to the same object
endfunction
function void display();
$display("Data = %0d",data);
$display("slv.a = %0d, slv.b = %0d", slv.a, slv.b);
endfunction
endclass
module inheritence;
initial begin
child_class c=c2#()::type_id::create("c", null);
c.addr = 10;
c.data = 20;
c.display();
end
endmodule