Use a function of registerd class in other task.

Assume that A is a registered class extended by uvm_object. B is a function inside A. Now in another task C,if I want to use the function B in A,can I code as below shown?

class A extends uvm_object;
  `uvm_object_utils(A)
  function void B();
  endfunction
endclass:A

task C;
  A U_A;
  U_A=A::type_id::create("U_A",this);
  U_A.B();
endtask:C

Yes, you should be able to do that.
You are creating an object U_A of type A by using “create” method. After that you will be able to access all the members of that class (A).

You cannot have a reference to this outside of a class method, if that was your intention. You don’t need a parent argument to create a uvm_object anyways, so you can eliminate it.

Sorry, I still don’t understand. What don’t we need a parent argument when creating uvm_object. If I want to register it ,this class should extend uvm_object.
By the way , can we instantiate a class in a task?

The parent argument to create() is a uvm_component. It sets the context used when there are factory overrides. A context is the hierarchical path of a tree of uvm_components. (if you were creating a class extended from uvm_component the argument sets both is context and its parent).

Typically you are constructing UVM classes as methods of other UVM classes. You would pass this as the parent if the class was extended from uvm_component. Otherwise it would be null, or if creating from a sequence, you would pass the sequencer.

1 Like

Thanks! I didn’t notice that I had added this when creating a child class of uvm_object.
Additionally, I still want to ask that if it is allowed to instantiate a class in a task?

Yes. Did you try it?

Thanks for your patient answering. I have tried and it worked.