interface_cast (C++ RTL Function)

From RAD Studio
Jump to: navigation, search

Go Up to New-style Typecasting


interface_cast is a template function defined in the System unit that can be used to query interfaces. It is similar to the as operator in Delphi (with the difference that interface_cast is not a built-in operator, but an RTL function).

You should use interface_cast with COM-style interfaces to ensure correct lifetime management of COM objects. A COM object has an associated reference counter, which is used to track the number of interface instances that execute calls on it. When creating new interface instances with interface_cast, the reference counters are incremented as appropriate (_AddRef is called).

The result of interface_cast must be tested before usage. If it is null, then the cast failed (this resembles the dynamic_cast situation).

See Also