自学内容网 自学内容网

关键字local、this、super、proteted作用域(Systemverilog)

SV中的super,this和local主要用来区分同名变量;

如果不使用此类前缀,SV会采用就近原则,索引到最近的变量。

区别分析

1. this和super

  • this和super一般用来区分子类与父类的同名变量

  • this.name一般指向当前类的成员变量,super.name则指向父类的成员变量。

2. local和protected

  • local表示本地变量,local声明的变量只能在本class中使用,subclass以及外部类中不可调用

  • protected比local限制少一些,protected声明的变量能在本class以及subclass中使用,外部类中不可

  • 使用错误会报一下信息:

Local/Protected member ‘xx’ of class ‘yyy’ is not visible to scope
‘zzzz’ . please make sure that the above member is accessed only
from its own class properties as it is declared as local/protected .

3. this和local

  • local::关键词可以索引到当前域(class, task, function)中的变量

  • 除了在声明成员变量时,在使用内嵌约束进行随机化时也会需要使用local关键字:

class test;
    rand int [31:0] data;

    task send(input data);
        trans  trans_new;  // trans中也有data变量。
        trans_new = new();
        void`(trans_new.randomize() with {data == local::data;}) 
      endtask
   endclass
  • local::data索引到的是形参data, 而不带前缀的data按就近原则索引到trans_new中的data。
  • 若send() 方法中没有参数传递,则local::data索引到的是当前类的变量rand data

原文地址:https://blog.csdn.net/Shu_0223/article/details/142489747

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!