I wanted to deep and shollow copy with dynamic array? it is possible and how

class inner;
	int i[];
endclass
class outer;
	int o[];
	inner in = new();
	function void copy (outer ou);
		this.o = ou.o;
		this.in = new ou.in;
	endfunction
	endclass
module c_copy();
outer obj1, obj2;
initial begin
 obj1 = new();
 obj2 = new();
 obj2.copy(obj1);
 obj1.o = 20;
 obj1.in.i = 30;
 $display(" obj1.o=%0d, obj1.in.i =%0d",obj1.o, obj1.in.i);//20,30
 $display(" obj2.o=%0d, obj2.in.i =%0d",obj2.o, obj2.in.i);//10,5
 end
endmodule

In reply to muku_383:

class inner;
	int i = 5;
	int j[];
endclass
class outer;
	int o = 10;
	int k[];
	inner in = new();
	function void copy (outer ou);
		this.o = ou.o;
		this.in = new ou.in;
	endfunction
	endclass
module c_copy();
outer obj1, obj2;
initial begin
 obj1 = new();
 obj2 = new();
 obj2.copy(obj1);
 obj1.o = 20;
 obj1.in.i = 30;
 $display(" obj1.o=%0d, obj1.in.i =%0d",obj1.o, obj1.in.i);//20,30
 $display(" obj2.o=%0d, obj2.in.i =%0d",obj2.o, obj2.in.i);//10,5
 end
endmodule

In reply to muku_383:

how this work?

In reply to muku_383:

Have you reviewed the link I gave you?

There is nothing special about copying an array of int versus a singular int. You just make a simple assignment like you did with o. Also, copy() should not do any class construction. Just call the inner copy.

class inner;
	int i = 5;
	int j[];
        function void copy(inner from);
            if (from == null) return;
            this.i = from.i;
            this.j = from.j;
        endfunction
endclass
class outer;
	int o = 10;
	int k[];
	inner in = new();
	function void copy (outer from);
                if (from == null) return;
		this.o = from.o;
		this.k = from.k;
                this.in.copy(from.in);
	endfunction
	endclass
module c_copy();
outer obj1, obj2;
initial begin
 obj1 = new();
 obj2 = new();
 obj2.copy(obj1);
 obj1.o = 20;
 obj1.in.i = 30;
 $display(" obj1.o=%0d, obj1.in.i =%0d",obj1.o, obj1.in.i);//20,30
 $display(" obj2.o=%0d, obj2.in.i =%0d",obj2.o, obj2.in.i);//10,5
 end
endmodule

P.S. Please use code tags.

In reply to muku_383:

I don’t understand if there is a problem in making a deep copy of the dynamic array the way you have coded. Can you please specify the error line or the issue more clearly?

In reply to @nurag:

In reply to muku_383:
I don’t understand if there is a problem in making a deep copy of the dynamic array the way you have coded. Can you please specify the error line or the issue more clearly?

hi,
i have doubt with dynamic array using in shallow copy, instead of using int i = 1;
i wanted to shallow copy of dynamic array above code is only for understanding. can you help me for same

In reply to dave_59:

In reply to muku_383:
Have you reviewed the link I gave you?
There is nothing special about copying an array of int versus a singular int. You just make a simple assignment like you did with o. Also, copy() should not do any class construction. Just call the inner copy.

class inner;
int i = 5;
int j[];
function void copy(inner from);
if (from == null) return;
this.i = from.i;
this.j = from.j;
endfunction
endclass
class outer;
int o = 10;
int k[];
inner in = new();
function void copy (outer from);
if (from == null) return;
this.o = from.o;
this.k = from.k;
this.in.copy(from.in);
endfunction
endclass
module c_copy();
outer obj1, obj2;
initial begin
obj1 = new();
obj2 = new();
obj2.copy(obj1);
obj1.o = 20;
obj1.in.i = 30;
$display(" obj1.o=%0d, obj1.in.i =%0d",obj1.o, obj1.in.i);//20,30
$display(" obj2.o=%0d, obj2.in.i =%0d",obj2.o, obj2.in.i);//10,5
end
endmodule

P.S. Please use code tags.

i watched all session.
thanks dave_59