Is this mandantory to use 'new' to functio

2019-09-20 02:13发布

Now I'm trying to study about clss of systemverilog.

From many class of example, I found the 'new' in 2 types.

  1. The case of the 'new' is existed in class.
  2. The case of the 'new' is existed in initial.

Is there any difference between those implementation of constructor?

One more, what is in the function new()? I'm not sure what purpose is in the function new()

update

For example 1 is. Class xxx ... Function new(); ... Endfunction

Endclass

Example2 is

program

class xxxx


 endclass

 Initial begin
 xxxx  x = new;
   end

endprogram

update 2

Thanks for let me know. I've got a question. What Is the difference between

Class xxx
...
Function new();
(Variable initialization)
...
Endfunction
Endclass

And

Class xxx
...
Function new();
(Nothing variable initialization)
Endfunction
Endclass

But in this case to pass the value in the intial statement or tasks. What is in the function new() endfunction? I'm not sure should I have to initialize the variables?

update3

class packet;
  //class properties
  bit [31:0] addr;
  bit [31:0] data;
  bit        write;
  string     pkt_type;

  //constructor
  function new(bit [31:0] addr,data,bit write,string pkt_type);
    addr  = addr;
    data  = data;
    write = write;
    pkt_type = pkt_type;
  endfunction

  //method to display class prperties
  function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction
endclass

module sv_constructor;
  packet pkt;
  initial begin
    pkt = new(32'h10,32'hFF,1,"GOOD_PKT");
    pkt.display();
  end
endmodule

This is what I've a code. you can see that,

two types declare of function block.

1. is with new

  function new(bit [31:0] addr,data,bit write,string pkt_type);
    addr  = addr;
    data  = data;
    write = write;
    pkt_type = pkt_type;
  endfunction


2. is without new.

  //method to display class prperties
  function void display();
    $display("---------------------------------------------------------");
    $display("\t addr  = %0h",addr);
    $display("\t data  = %0h",data);
    $display("\t write = %0h",write);
    $display("\t pkt_type  = %0s",pkt_type);
    $display("---------------------------------------------------------");
  endfunction

1条回答
等我变得足够好
2楼-- · 2019-09-20 02:20

Calling the new() method of a class is the only way to construct a class object. There are a few reasons you might want to define a class and never call the new() method on it, but you should ask that question later when you have a better understanding of SystemVerilog.

Update

I think you may be asking what is the difference between a class with declared function new() inside the class

class xxxx;
  int x;
  function new;
  ...
  endfucntion
endclass

versus a class without it

class xxxx;
  int x;
endclass

If you do not declare a function new() inside your class, SystemVerilog defines an implicit one for you. The reason you might want to declare a function new inside your class is if you want to pass in arguments to the constructor, or you have something that requires more complex procedural code to initialize.

查看更多
登录 后发表回答