When a $cast is executed between a base and a derived class objects, does it allocate more memory for the derived class handle?

$cast(derived, base) Upon a successful casting, the handle would be able to access derived class members also. So, how exactly do the internals work?

  • 1
    \$\begingroup\$ Could you please give a specific example that illustrates your question? Are you talking about synthesizable code? \$\endgroup\$ – Elliot Alderson Sep 29 '18 at 20:24
  • \$\begingroup\$ I do not mean synthesizable code. Straight up systemverilog for verification purposes. A simple example below - parent_handle = new(); child_handle = parent_handle; // causes error $cast (child_handle, parent_handle); //works now If there was some member unique to the child class, it will now be accessible which was not possible before. So, did more memory get allocated to the child handle following $cast? \$\endgroup\$ – kernalmode Sep 29 '18 at 20:35

A $cast works like any other assignment: it transfers a value from a source expression to a destination variable. In the case of $cast, that value is a handle to a class object, and it is being conditionally assigned to a class variable. There is no class object construction. The condition is the class object has to be type compatible with the class type of the destination class variable. The type of the class variable determines what properties may be accessed, not the type of object handle. The casting and assignment rules make sure it is not possible to access a class property that does not exist for the handle stored in any class variable.

See my course on SystemVerilog classes, especially the second session.

  • \$\begingroup\$ Would you be so kind to explain the difference between "object handle" and "class variable". I am under the impression that both the arguments to $cast are just object handles, one a parent type and another the child type. \$\endgroup\$ – kernalmode Sep 30 '18 at 5:31
  • \$\begingroup\$ See the link above, as well as go.mentor.com/class-on-classes \$\endgroup\$ – dave_59 Sep 30 '18 at 14:24
  • \$\begingroup\$ After some code writing and debug, I understand the concept \$\endgroup\$ – kernalmode Oct 3 '18 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.