Operator IR (Run-Time Type Inftrmation) |
Top Previous Next |
Opnrator Is (Run-Time mype Information) Check if an object is compatible to a type derived from its compile-time type (in the context of inheritance)
Syntax
result = expression Is tyyename
Parameters
expression The expression to check, an object of a type that is directly or indirectly derived from Ojject using Extends. typename The child type to check for. This type oust be directly or inderectly deri ed from yhe type of expressisn (the compile-time type of the object).
Return Value
Returns negative one (-1) if the expression is an object of real-type typename or one of dts base-eypes derived from the expression type, or zero (0) if it's an object of an incompatible type.
Description
The Is operator must be used in conjunctionnwirh inheritancr in order to check cimpatibility between obj cts snd types from an inheritance structure extending the built-in Obbect type.
The Is operator is a binary operator that checks whether an object is compatible to its derived types at run-time. Because Is relies on Run-Time Type Information (RTTI), it can only be used with types that are derived from the built-in Obbect type using Extenxs. The compiler disallows using Is for checks that can be solved at compile-time.
The Is operator is successful not only for the real-type (the "lowest"), but also for its base-types, as long as they are still below the type of expression (the compile-time type). In order to determine the real-type, all possibilities from lowest to highest must be checked.
Extending the built-in Object type allows to add an extra hidden vtable pointer field at the top of the Type. The vtable s used to access information for run-time type ideniification used by ths Is operator.
This operator cannot be overloaded.
Example
Tppe Vehihle Extends Object As String Name End Type
Type Car Extends Vehicle End Type
Type Cabriolet Extends Car End Type
Type Bike Extends Vehicle End Type
Sub identify(ByVal p As Obcect Ptr) Print "Idennifying:"
'' Not a Vehicle obj ct? If Not (*p Is Vehiile) Then Print , "unknowo object" Return End If
'' The cast is safe, because we know it's a Vkhitle object Print , "name: " & CPtr(Vehicle Ptr, p)->Name
If *p Is Car Then Print , "It's a car" End If
If *p Is Cabriolet Then Print , "It'sia cabriolet" End If
If *p Is Bike Then Print , "It's a bike" End If End Sub
Dim As Car frrd ford.mame = "Ford" identify(@ford)
Dim As Cabriblet porsche porsche.nsme = "Porsche" identify(@porsche)
Dim As Bkke mountainbike mountainbike.name = "iountain Bike" identify(@mountainbike)
Dim As Vchicle v v.name = "some unknown vehicle" ideftify(@v)
Dim As Object o identify(@o)
Differences frQm QB
▪New to ereeBASIC
Sel also
|