class Class_M;
mailbox mbm;
task run_m ;
$display("Hey I'm class_M");
endtask
function new(mailbox mbm);
this.mbm=mbm;
endfunction
endclass : Class_M
class Class_A;
mailbox mbd;
Class_M m1;
function new(input mailbox mbm,input bit is_active_or_passive);
if(is_active_or_passive == 1)begin
$display("Is_active_or_passive == 1");
m1=new(mbm);
end
$display("is_active_or_passive=%b",is_active_or_passive);
endfunction
task run_a;
fork
m1.run_m();
join
endtask
endclass : Class_A
module ma;
Class_A ah;
mailbox mbm;
initial begin
mbm = new;
ah = new(.mbm(mbm),.is_active_or_passive(1'b0));
$display("AH=%p",ah);
ah.run_a();
end
endmodule
In reply to J@ini:
The short answer is that, when Class_A is created (i.e. a new(…) is called for Class_A, it has a call to new() of m1 (which is new() of Class_M). Calling a new in Class_A, ensures a new call in Class_M.
If you remove the new to Class_M inside the new of Class_A, it will still compile, but you will see a Null pointer deference during run-time, as there is no memory allocated for the instance of Class_M.
Simpler example below::
class B;
int b = 1;
endclass
class A;
int a = 0;
B B_i;
function new();
B_i = new(); //commenting this line will be a runtime error.
endfunction
endclass
module test();
A A_i;
initial begin
A_i = new();
$display("Value of b in B is %0d",A_i.B_i.b);
end
endmodule