is-operator

Top  Previous  Next

What is translated > Operators > is-operator

In Delphi, the is operator is used to test whether an object is an instance of a given class or one of its descendants.

 

For example:

 

if Obj is TMyClass then ...

 

This is a runtime type check. It is commonly used for safe downcasting, conditional logic based on class type, and for testing objects that were obtained indirectly, for example through interface references.

 

Scope of translation

 

In Delphi2Cpp, the is operator is translated into C++ helper functions or macros that preserve Delphi semantics as closely as possible.

 

Depending on the form of the original Delphi expression, different translations are used.

 

1) Object against a fixed class type

 

When the right-hand side is a concrete class name, Delphi2Cpp translates:

 

Obj is TMyClass

 

to either of the following C++ forms:

 

IsInstanceOf<TMyClass>(Obj)

 

or

 

ObjectIs(Obj, TMyClass*)

 

The macro ObjectIs is defined as:

 

#define ObjectIs(OBJ, CLASSPTR) (dynamic_cast<CLASSPTR>(OBJ) != nullptr)

 

This is the most direct equivalent for ordinary object type checks.

 

2) Object against a runtime class reference

 

In Delphi, the is operator may also be used with a class reference stored in a variable:

 

Obj is Cls

 

where Cls is of type TClass.

 

In this case, Delphi2Cpp translates the expression to:

 

IsInstanceOf(Obj, Cls)

 

This corresponds to a Delphi-style runtime class test based on InheritsFrom.

 

 

3) Interface back to object, then class test

 

Delphi also allows constructs such as:

 

TObject(Intf) is TMyClass

 

Here the interface reference is first converted back to the implementing object, and the class check is then applied to that object.

 

In C++, Delphi2Cpp translates this pattern as:

 

ObjectIs(FromInterface(Intf), TMyClass*)

 

or equivalently:

 

IsInstanceOf<TMyClass>(FromInterface(Intf))

 

FromInterface(...) reconstructs the underlying TObject* from an interface reference and is required whenever a class-based is test is applied to an interface value.

 

 

4) Exact class comparison

 

Delphi distinguishes between is and exact class equality.

 

For example:

 

Obj.ClassType = TMyClass

 

This does not test for descendants, but for an exact type match only.

 

In C++, Delphi2Cpp translates this to:

 

IsExactClass(Obj, class_id<TMyClass>())

 

In generated code, this may also appear directly as:

 

Obj->ClassType() == class_id<TMyClass>()

 

This corresponds exactly to Delphi's ClassType = ... semantics.

 

Summary

 

Delphi expression

C++ translation

Obj is TMyClass

IsInstanceOf<TMyClass>(Obj) or ObjectIs(Obj, TMyClass*)

Obj is Cls

IsInstanceOf(Obj, Cls)

TObject(Intf) is TMyClass

ObjectIs(FromInterface(Intf), TMyClass*)

Obj.ClassType = TMyClass

IsExactClass(Obj, class_id<TMyClass>())

       

 

Notes

 

Use ObjectIs(...) or IsInstanceOf<T>(...) for ordinary object checks.

Use IsInstanceOf(obj, Cls) when the target is a runtime TClass.

Use FromInterface(...) before performing class checks on interface references.

If the target of the check is an interface type rather than a class type, Delphi2Cpp uses Supports(...) instead of is.

 

Note on meta capabilities

 

The functions IsInstanceOf(...) and IsExactClass(...) rely on Delphi-compatible runtime type information.

To use these functions, the meta capabilities option must be enabled in Delphi2Cpp.

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content