通过引用传递更新构造函数中的 类' 变量?

Updating a classes' variable in a constructor through pass by reference?

凭借对 SystemVerilog 内部工作原理的新知识,我已经着手使用这些狂热的传递引用功能之一来更新另一个[=22]的构造函数中的 classes' 计数器=].设置(剥离到基础)看起来有点像这样:

class my_queue;
  int unsigned num_items; //Want to track the number of items this Queue has seen.

  function push_new_item();
     item new_item = new(num_items);
  endfunction

endclass

class parent_item;
   int unsigned x_th_item;
   function new(ref int unsigned num_items);
      x_th_item = num_items;
      num_items += 1; //This should increase the counter in num_items.
   endfunction
endclass

class item extends parent_item;
   function new(ref int unsigned num_items);
      super.new(num_items);
   endfunction
endclass

问题是我的编译器抱怨

Illegal connection to the ref port 'num_items' of function/task parent_item::new, formal argument should have same type as actual argument.

我有一个关于如何解决这个问题的想法:在 push_new_items 中调用 new() 后移动增量。
但是我仍然不知道如何在 SV 中正确使用 pass-by-refrence 那么是什么导致了错误?
是另一个传递引用还是语法错误?

Qiu 确实指出了我的代码存在的问题。我的问题是,虽然在两端都正确声明了变量,但我的一个构造函数是这样写的:

function new(ref int num_items);

它应该在的地方

function new(ref int unsigned num_items);

谢谢秋

你不需要 ref 语义,使用 inout 参数。 inout 在任务或函数的 return 进入时被复制入并被复制出。正如您在 ref 参数中看到的那样,类型兼容性要求要严格得多。

唯一必须使用 ref 参数的情况是在耗时的任务中,您需要在任务 returns 之前查看参数的活动更新。

task my_task(ref bit tclock);
  @(posedge tclock) // this would hang if tclock was an input
endtask 

您可能希望使用 ref 参数的另一个地方是当参数类型是大型对象(如数组)时作为优化。但是通过引用传递单个 int 实际上比直接复制它的值要慢。