In below code, I am not sure why it is not necessary to create an instance of f in line 10.
class foo;
function void build();
$display(" i am build");
endfunction
endclass
class cmd;
foo f;
function void setF(foo f);
this.f = f;
endfunction
function void run();
f.build();
endfunction
endclass
module top;
cmd d;
foo f ; --> line 10. Do we need f = new() ?
initial begin
d = new();
d.setF(f);
d.run();
end
endmodule
In reply to VE:
Have you tried it?
What to you expect f.build() to do?
Thanks Dave.
" Have you tried it ?"
– Yes, i did run 3 simulators. i got the message “i am build” from the function f.build() without creating an object of foo ( f = new()) from all 3.
" What to you expect f.build() to do "
– My code is just a small test case to demo my question, basically i want to use the command pattern in my TB. It seems to me there is unnecessary to have f = new(); i am not sure it is related to C++ style? “static” ? or …?
Thanks
From that thread, the 2017 LRM quote at play here is section 8.4:
Accessing non-static members (see 8.9) or virtual methods (see 8.20) via a null object handle is illegal. The result of an illegal access via a null object is indeterminate, and implementations may issue an error.
I might guess the second part is in there for performance reasons, as the simulator doesn’t want to constantly perform a null-check every time a non-static member is accessed?