I’m trying to understand how much create() have the benefits instead new().
So I make a simple example to compare between them.
This is created by new() method.
// Define a base class
class my_base_class extends uvm_object;
// Define some virtual methods
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// Define a derived class
class my_derived_class_1 extends my_base_class;
// Override the virtual method
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// Define another derived class
class my_derived_class_2 extends my_base_class;
// Override the virtual method
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// In the testbench, create objects of different types using new()
module my_testbench;
// Create an object of my_derived_class_1
my_base_class* obj1 = new my_derived_class_1();
// Create an object of my_derived_class_2
my_base_class* obj2 = new my_derived_class_2();
// Call a virtual method on the objects
obj1.do_something();
obj2.do_something();
endmodule
This is created by create() method.
// Define a base class
class my_base_class extends uvm_object;
// Define some virtual methods
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// Define a derived class
class my_derived_class_1 extends my_base_class;
// Override the virtual method
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// Define another derived class
class my_derived_class_2 extends my_base_class;
// Override the virtual method
virtual function void do_something();
// Implementation not shown
endfunction
endclass
// In the testbench, create objects of different types at runtime
module my_testbench;
// Create an object of my_derived_class_1
my_base_class* obj1 = my_derived_class_1::type_id::create("my_derived_class_1", null);
// Create an object of my_derived_class_2
my_base_class* obj2 = my_derived_class_2::type_id::create("my_derived_class_2", null);
// Call a virtual method on the objects
obj1.do_something();
obj2.do_something();
endmodule
From verificationacademy.com, the create() allows type overriding during runtime and no need to modify the code .
I’m curious as to what the advantages are from a hardcoding perspective only.