从嵌套类访问父类变量

Sta*_*low 0 oop class system-verilog

我想从嵌套类访问变量宽度/高度/大小,将静态放在它们前面是有效的,但是还有另一种方法吗?

class random_messages; 

    int                 max_x;
    int                 max_y;
    rand int            width; 
    rand int            height;
    rand int            size; 

    class rand_x;
        randc int  loc_x;        
        constraint sizes {
            loc_x  < width / 2**(size+3); //accessing here
            loc_x  > 0;
        }        
    endclass

endlcass
Run Code Online (Sandbox Code Playgroud)

Tud*_*imi 5

不要认为仅仅因为您rand_x在 class 中定义了 class random_messages,就自动意味着嵌套类的对象在包装类的对象中被实例化。声明一个嵌套类只会改变它定义的范围。

在您的情况下,如果您想访问父对象的变量,则必须执行以下操作:

(在嵌套类中)声明父级的句柄并将父级作为构造函数参数传入:

class rand_x;
  // ...

  protected random_messages m_parent;

  function new(random_messages parent);
    m_parent = parent;
  endfunction
endclass
Run Code Online (Sandbox Code Playgroud)

(在外部类中)声明内部类的一个实例并将自己作为其父类传递:

class random_messages;
  // ...

  rand rand_x x;

  function new();
    x = new(this);
  endfunction
endclass
Run Code Online (Sandbox Code Playgroud)