IEEE Std 1800-2012 & sect; 8.16 "Casting":
It is always the right to assign an expression of a subclass type to a variable of a class type higher in the inheritance tree (superclass or ancestor of the expression type). The direct assignment of a variable of the type of a superclass to a variable for one of its subclass types is unacceptable. However, $cast can be used to assign a superclass descriptor to a variable of a subclass type if the superclass descriptor refers to an object that is an assignment compatible with the subclass variable.
The following actions are not performed because the superclass object is not read as a child class.
m_base = new(); $cast(m_extend, m_base); // destination type != source object type
In order to correctly apply the source descriptor object, it must be compatible with the destination type, which must be comparable:
m_extend = new(); m_base = m_extend; $cast(m_extend, m_base); // destination type == source object type
Downcasting can work through inheritance levels. The following example demonstrates a base class descriptor that points to a grandson object passed to the extend class (the parent class of the grandchild object):
class ext_more extends extend; int c; endclass initial begin base m_base; extend m_extend; ext_more m_ext_more; m_ext_more = new(); m_base = m_ext_more; $cast(m_extend, m_base);
Here are some working examples: http://www.edaplayground.com/s/6/587
source share