quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Assignments

Assignment of one object to another object is same as assigning a variable to another variable of other data tyes. In object assignment, the copy is a shallow copy because it does not make a copy of any nested objects. For making deep copy, custom tasks are required. Below example shows how to do custom copy.

   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Example - Assignments
   

space.gif


  1 program class_copy;
  2 
  3   class A ;
  4     integer j = 5;
  5     task print();
  6       begin
  7         $display("j is %0d",j);
  8       end
  9     endtask
 10   endclass
 11 
 12   class B ;
 13     integer i = 1;
 14     A a = new;
 15     task print();
 16       begin
 17         $display("i is %0d",i);
 18         a.print();
 19       end
 20     endtask
 21     // custom deep copy task
 22     task copy(ref B bb);
 23       begin
 24         bb = new this;
 25         bb.a = new this.a;
 26       end
 27     endtask
 28   endclass
 29   
 30   initial begin
 31     B b1,b2,b3;
 32     $display("Testing shallow copy");
 33     b1 = new; // Create an object of class B
 34     b1.print();
 35     b2 = new b1; // Create an object that is a copy of b1
 36     b2.print();
 37     b2.i = 10; // i is changed in b2, but not in b1
 38     b2.a.j = 50; // change a.j, shared by both b1 and b2
 39     b2.print();
 40     b1.print(); // Updated due to shallow copy
 41     // Now do a deep copy
 42     $display("Testing deep copy");
 43     b1.copy(b3) ; // Create an object that is a deep copy of b1
 44     b3.print();
 45     b3.i = 100; // i is changed in b3, but not in b1
 46     b3.a.j = 500;// j is changes in b3, but not in b1
 47     b3.print();
 48     // i and J should not change due to deep copy
 49     b1.print();
 50   end
 51 
 52 endprogram
You could download file class_copy.sv here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation - Assignments
   

space.gif

 Testing shallow copy
 i is 1
 j is 5
 i is 1
 j is 5
 i is 10
 j is 50
 i is 1
 j is 50
 Testing deep copy
 i is 1
 j is 50
 i is 100
 j is 500
 i is 1
 j is 50
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com