Constructor

Hi,

I have 2 questions:

(i) When we extend a class ‘A’ from uvm_component then to create the object of class ‘A’ we need to call the constructor of uvm_component ,can i directly call new method of uvm_component class i.e, instead of calling create method i will be calling new method of class ‘A’ (where class ‘A’ is not having any new method , since it is extending from uvm_component it should call new method of uvm_component). If not, then why?

(ii) Since constructor of uvm_component doesn’t have any default argument then one way to pass the argument value is by calling super.new(n,p) and 2nd way is by specifying argument in the extend clause , i didn’t understand the 2nd way of doing it.

In reply to Shipra_s:

(i) Every class in SystemVerilog has a new() method constructor. If you don’t code one explicitly, SystemVerilog inserts one for you implicitly. You must call new() to construct a class object. The create() static method is defined by the UVM and it calls new() for you. Please see https://www.edn.com/design/systems-design/4461407/Inheritance-and-polymorphism-of-SystemVerilog-OOP-for-UVM-verification?rssid=2618c3e4-c775-f2c5-f5f7-d8058ff2b4c3

(ii) Do not use the 2nd way. Experiment with it if you want to learn more, but it is never used with the UVM.

In reply to dave_59:

Thanks Dave, i understand that create() method calls new() method , but i wanted to know, can i directly call new method (here i am not using create method) of uvm_component class to create an object of class ‘A’ which extends from uvm_component??

Like this :

class A extends uvm_component;

endclass

class B extends uvm_component;
A a1;
function void build_phase(uvm_phase phase);
a1 = new(“a1”,this)
endfunction
endclass

Here when i’am trying to do this,
I am getting this error:

** Error: …/src/Level0.svh(2): Super class constructor has non-default arguments. Arguments can be specified in the “extends” clause or by calling super.new() explicitly.

In reply to Shipra_s:

class A extends uvm_component;
function new(string name, uvm_component parent);
  super.new(name,parent);
endfunction
...
endclass

class B extends uvm_component;
function new(string name, uvm_component parent);
  super.new(name,parent);
endfunction
A a1;
function void build_phase(uvm_phase phase);
a1 = new("a1",this)
endfunction
endclass

In reply to dave_59:

So, here the question is if i don’t write the new function in class A then, a1 = new(“a1”,this) statement should call the new function of uvm_component, correct me if i am wrong here.

In reply to Shipra_s:

Incorrect. If you don’t code a constructor explicitly, SystemVerilog inserts one for you implicitly. If you do not call super.new as the first statement of a constructor, SystemVerilog inserts one for you.

class A extends uvm_component;
function new(); // implicitly inserted
  super.new();
endfunction
...
endclass

In reply to dave_59:

Thanks Dave, i got my answer.