In reply to dave_59:
$cast is a dynamic cast. That means it will check at runtime if the assignment is legal. A static cast, sometype’(expr), or an assignment is checked for validity at compilation.
The assignment from
child_handle to
parent_handle is always allowed, but an assignment the reverse direction can only be checked at runtime using $cast. Suppose you had:
initial begin
parent_handle = new();
child_handle = parent_handle; // not legal
$display("I am child with member of value %d",child_handle.foo_mem);
end
The reference to
child_handle.foo_mem is only valid if
child_handle contains a handle to a
child_ class type.
Sometimes you do know know what kind of object has been stored in parent_handle because it has been passed down as a function argument. You can use $cast to check the kind of object you have.
The answers to your other questions are a basic part of the object oriented programming principles of SystemVerilog. Just do a search of “virtual methods in SystemVerilog”
...
if ($cast(child_handle,parent_handle)) begin
$display("I can display %d",child_handle.foo_mem);
else
$display("I don't have a child_ class type");
...
How does $case(destination,source) work exactly?
class parent_;
virtual function foo_;
end
class child_ extends parent_;
bit foo_mem;
function void foo_();
$display("I am child with member of values %d",foo_mem);
endfunction
endclass
module foo_foo;
parent_ parent_handle;
parent_ parent_handle_1;
child_ child_handle;
initial begin
child_handle = new();
$cast(parent_handle,child_handle);//
parent_handle_1 = child_handle; // What is the difference between this line and the $cast above ?
parent_handle_1.foo();
parent_handle.foo_();
end
endmodule
Would you please let me know any equivalence code of
$cast(parent_handle,child_handle);//
for understanding?
And also,
The reference to child_handle.foo_mem is only valid if child_handle contains a handle to a child_ class type.
Sometimes you do know know what kind of object has been stored in parent_handle because it has been passed down as a function argument. You can use $cast to check the kind of object you have.
From here, Would you please let me know more about
child_handle contains a handle to a child_ class type
meaning?
If it should be, what does it suppose to do ?
Can you show me any examples for something handle contains a something class type?