这是强制使用'new'在systemverilog类中起作用吗? [英] Is this mandantory to use 'new' to function in the class of systemverilog?

查看:20
本文介绍了这是强制使用'new'在systemverilog类中起作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在研究 systemverilog 的类.

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

从许多示例中,我发现了 2 种类型的新".

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

  1. 班级中存在新"的情况.
  2. 新"的大小写在初始存在.

这些构造函数的实现有什么区别吗?

Is there any difference between those implementation of constructor?

再补充一点,new() 函数里有什么?我不确定 new() 函数的目的是什么

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

例如 1 是.xxx班...函数 new();...结束函数

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

终结类

示例 2 是

program

class xxxx


 endclass

 Initial begin
 xxxx  x = new;
   end

endprogram

更新 2

谢谢你让我知道.我有一个问题.

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

还有

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

但是在这种情况下要在初始语句或任务中传递值.函数 new() 结束函数中有什么?我不确定是否应该初始化变量?

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?

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,

功能块的两种声明.

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

推荐答案

调用类的 new() 方法是构造类对象的唯一方法.您可能想要定义一个类并且从不对其调用 new() 方法的原因有几个,但是当您对 SystemVerilog 有了更好的理解后,您应该问这个问题.

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.

我想你可能会问在类中声明函数 new() 的类有什么区别

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

对比没有它的班级

class xxxx;
  int x;
endclass

如果你没有在你的类中声明一个函数 new(),SystemVerilog 会为你定义一个隐式的.您可能想要在类中声明一个 new 函数的原因是,如果您想将参数传递给构造函数,或者您有一些需要更复杂的程序代码来初始化的内容.

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.

这篇关于这是强制使用'new'在systemverilog类中起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆