SV中的shadow copy和deep copy的区别

SV中shadow copy和deep copy的区别

1.1 shadow copy ,是一种简易复制,类似于原对象的影印本,只拷贝原对象的内容,不拷贝对象(用new操作符)

class Transaction;
        bit[31:0]addr,crc,data[8];
endclass

Transaction src,dst;
initial begin
        src=new;//创建第一个对象
        dst=new src//使用new操作符进行复制
        end

1.2如果类中包含一个指向另一个类的句柄,那么只有高一级的对象(变量和句柄)被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 begin
     src=new();
     src.stats.startT=42;
     dst=new src;
     dst.stats.startT=96;
     display(src.stats.startT);

在使用new操作符进行拷贝之前,dst为空;使用new操作符进行复制之后,src中变量和句柄的值被复制(但是不会调用new函数,所以两个Transaction对象的id值相同,并且指向同一个statistics对象!)
使用new操作符进行复制之后的对象和句柄
使用new操作符进行复制之后的对象和句柄

2、 deep copy 可将拷贝对象中所包含的对象拷贝过来(自定义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++;
      endfunction

      function 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 begin
     src=new();
     src.stats.startT=42;
     dst=src.copy();
     dst.stats.startT=96;
     $display(src.stats.startT);
   end

在这里插入图片描述
经过深拷贝后的对象和句柄

猜你喜欢

转载自blog.csdn.net/yu1216338826/article/details/86551128