转载自:https://www.verificationguide.com/p/systemverilog-casting.html
SystemVerilog Casting
casting
Dynamic casts can be used to safely cast a super-class pointer (or reference) into a pointer (or reference) to a subclass in a class hierarchy. If the cast is invalid because the real type of the object pointed to is not the type of the desired subclass, the dynamic will fail gracefully.
Lets see how we can use the casting,
It is always legal to assign a child class variable to a variable of a class higher in the inheritance tree (parent class).
parent_class = child_class; //allowed
It is never legal to directly assign a super-class (parent class) variable to a variable of one of its sub classes (child class).
child_class = parent_class; //not-allowed
However, it is legal to assign a super-class (parent class) handle to a subclass (child class) variable if the super-class (parent class) handle refers to an object of the given subclass(child class).
parent_class = child_class ;
child_class = parent_class; //allowed because parent_class is pointing to child_class.
Though parent_class is pointing to the child_class, we will get a compilation error saying its not compatible type for the assignment.
This we can over come by make use of $cast method, i.e,
$cast(child_class,parent_class);
Example - 1
assigning child class handle to parent class handle
|
|
Simulator Output
Addr = 10
Data = 20
Execute the above code on
Example - 2
assigning parent class handle to child class handle, lead to compilation error.
|
|
Simulator Output
"c = p;"
Expression 'p' on rhs is not a class or a compatible class and hence cannot
be assigned to a class handle on lhs.
Please make sure that the lhs and rhs expressions are compatible.
Execute the above code on
Example - 3
assigning parent class handle(which is pointing to child class handle) to child class handle, lead to compilation error.
|
|
|
Simulator Output
"c1 = p;"
Expression 'p' on rhs is not a class or a compatible class and hence cannot
be assigned to a class handle on lhs.
Please make sure that the lhs and rhs expressions are compatible.
Execute the above code on
Example - 4
with the use of $cast(), type check during compile time can be skipped.
|
Simulator Output
Addr = 10
Data = 20
?红色字体?