嵌入式技术
假设有一个类“packet”,它含有一个static属性(或方法)“my_packet”,然后就可以从类外部访问使用类范围解析运算符(::)访问。
Packet::my_packet
需要注意的是“.”访问的是隶属于某个对象实例的成员,而“::”访问的是隶属于这个类的成员。
例如有一个类型为My_class的对象实例“mc” 。
mc.my_x访问的是对象实例的成员my_x;
My_class::my_x访问的是类的成员my_x;
class packet;
bit [31:0] addr; //non-static var
static bit [31:0] id; //static var
typedef enum {RED, GREEN, BLUE} RGB;
//typedef enum is static by default
extern static function void display (int a, b);
endclass
static function void packet::display (int a, b);
$display("packet values a=%0d b=%0d",a,b);
endfunction
class eth_packet extends packet;
function new;
packet::addr = 'hff;
// Can access non-static members in derived class
$display("packet addr = %h", packet::addr);
endfunction
endclass
module sro_class;
packet::RGB r1; //scope resolution operator for typedef
int id=10;
initial begin
packet p;
eth_packet ep;
p = new( );
ep = new ( );
$cast(r1,1);
$display("%s",r1);
packet::id = 20;
packet::display(packet::id, id);
//packet::addr = 'hf; //COMPILE ERROR
//Can't access non-static members outside the class
end
endmodule
仿真log:
packet addr = 000000ff GREEN packet values a=20 b=10 V C S S i m u l a t i o n R e p o r t
在上面的例子中,类 “packet” 中包含non-static属性(“addr”)和static属性 (“id”) 。
从仿真log可以看出可以在扩展类“eth_packet”内使用类范围解析运算符(::)访问非static属性 (“addr”)
而在类的外部访问非static属性 (“addr”)就会导致编译错误。
packet::addr = 'hff;
编译错误:
Error-[SV-IRTAV] Illegal reference to automatic variable testbench.sv, 34 "$unit::addr" Hierarchical reference to automatic variable 'addr' is not legal.
默认情况下,除了可以使用类范围解析运算符(::)访问静态属性和静态方法,还可以访问
– Static properties – Static methods – Typedefs – Enumeration – Structures – Unions
另外,这个类范围解析运算符(::)的作用可以只需要在类中定义方法的模版,具体的实现可以在类的外部:
class packet;
//function prototype
extern static function void display (int a, b);
endclass
//function body outside the class
static function void packet :: display (int a, b);
$display("packet values a=%0d b=%0d",a, b);
endfunction
这个extern方法可以访问类中的一些属性声明。
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !