1.浅拷贝
浅拷贝: 只拷贝对象中的数据变量,而对于对象中的数据操作(一般为任务和函数)和其中定义的其他类的句柄,采用类似“引用”的方式,浅拷贝前后共用同一内存空间。
可以使用new
操作符进行复制。如果一个类包含指向另一个类的句柄,只有最高级的对象被new操作符复制,下一层的对象都不会被复制
class Transaction;bit [31:0] addr,crc,data[8];statistics stats;static int count=0;int id;function new;stats=new();id=count++;endfunction
endclass Transaction src,dst;initial beginsrc=new();src.stats.startT=42;dst=new src;dst.stats.startT=96;display(src.stats.startT);
可以看到,具有相同的id地址,采用类似“引用”的方式,浅拷贝前后共用同一内存空间。
对p2中的a1句柄做操作后将会影响p1中的a1
2.深拷贝
深拷贝: 对象拷贝,对对象中的所有成员变量(包括数据变量、数据操作和其他句柄)统一分配新的内存空间。
可将拷贝对象中所包含的对象拷贝过来,自定义copy函数
,copy
调用了new函数
。
class Transaction;bit [31:0] addr,crc,data[8];statistics stats;static int count=0;int id;function new;stats=new();id=count++;endfunctionfunction Transaction copy;copy=new();copy.addr=addr;copy.data=data;copy.crc=crc;copy.stats=stats.copy();id=count++;endfunction
endclass Transaction src,dst;initial beginsrc=new();src.stats.startT=42;dst=src.copy();dst.stats.startT=96;$display(src.stats.startT);end
对p2中的a2句柄做操作后将会不会影响p1中的a1
参考:
https://blog.csdn.net/weixin_42162448/article/details/121972033
systemverilog验证测试平台编写指南