Correct way to create an object inside another object

I have an ovm_object class which instantiates other ovm_objects. What is the correct way to create the sub objects? I tried creating them inside the new() function but that showed an interesting problem.


//Lets say I have a parent object class
class parent_obj extends ovm_object;
ovm_object_utils(parent_obj)

rand child_obj ch1,ch2;

function new( string name="parent_obj");
super.new(name);
// Do I create child objects here? The name argument always takes the default value
ch1 = child_obj::type_id::create("child1",,name);
ch2 = child_obj::type_id::create("child2",,name);

endfunction
endclass

If I instantiate parent_obj as
p1 = parent_obj::type_id::create(“type_A_parent_obj”);

The name argument will still be parent_obj for the children.

To give some background, I have multiple instances of parent obj and I am trying to do set_inst_override for child objects of one parent obj only, which is why I need to provide name in the context field of create function

In reply to prang:

If this is a sequence , then “create” should be called inside the body task.

Components on the other hand should be “created” in build function .

In reply to dshetty030:

This is not a sequence but a configuration object of type ovm_object. Currently I got around this by creating a new function called build_objects which creates all the sub objects.