Navigating the Initialization Maze: Unraveling the Order of Execution in SystemVerilog Classes
In the below code a member of a class is initialised in a fun way
A a = A::build(this)
Static function is called at declaration of a variable which takes "this" object reference to get access to other variables of "this" object. Take a look at the code and answer what values string variables of class A object will hold at the end?
class P;
string name;
function new(string name);
this.name = name;
endfunction
endclass
typedef class B;
class A;
string name;
string name1;
function new();
endfunction
static function A build(B h);
A _a = new();
_a.name = h.name;
_a.name1 = h.name1;
return _a;
endfunction
endclass
class B extends P;
string name1;
A a = A::build(this);
function new(string name);
super.new(name);
this.name1 = "test";
endfunction
endclass
class TEST;
B b;
task run();
b = new("b_obj");
$display("a.name=%0s", b.a.name);
$display("a.name1=%0s",b.a.name1);
endtask
endclass
program top;
TEST t;
initial begin
t = new();
t.run();
end
endprogram
The line A a = A::build(this); is part of the class member initialization. This line is executed when an object of class B is created, and it's executed after the super.new(name) call in the constructor of class B. However, it's important to note that this line is executed during the initialization phase, and the constructor of class B has not completed its execution at this point.
When A::build(this) is called, it's called with a partially initialized object of class B. The this pointer refers to the current instance of class B, but the constructor of class B has not yet finished its execution. Therefore, the name1 member of class B has not been initialized at the time A::build(this) is called. But name member of parent class P has already been initialised.
领英推荐
In SystemVerilog, the order of execution during the creation of an object involves several steps. Let's break down the order of execution in the above code:
P.S. Large language model helped me to come up with the title for this article ??