Menu

Dynamic Cast

Mark Anthony Taylor (Shyreman)

Class instances can potentially implement more than one interface. Given a reference to one
such interface, it is possible to obtain a reference to the other interfaces by means of a dynamic
cast. This syntax is:

(cast <instance-name> -> <required-interface-name> <new-interface-variable-name>)

Where <instance-name> is the name of an existing interface variable.
And <required-interface-name> is the name of the new interface type.
And <new-interface-variable-name> is the name of the new interface variable.</new-interface-variable-name></required-interface-name></instance-name>

The cast operation will attempt to find the alternative interface and assign a reference to the
variable with name new-interface-variable-name.

If the interface with name required-interface-name is not found in the object to which the instance
with name <instance-name></instance-name> refers, then the interface variable with name <new-interface-variable-name>
will refer to the universal null object.</new-interface-variable-name>

-- William Shakespeare

Example:
(cast entity -> IRobot robot)

Related

Wiki: Content