Implementing IInterface

From RAD Studio
Jump to: navigation, search

Go Up to Using Interfaces


Just as all objects descend, directly or indirectly, from TObject, all interfaces derive from the IInterface interface. IInterface provides for dynamic querying and lifetime management of the interface. This is established in the three IInterface methods:

  • QueryInterface dynamically queries a given object to obtain interface references for the interfaces that the object supports.
  • _AddRef is a reference counting method that increments the count each time a call to QueryInterface succeeds. While the reference count is nonzero the object must remain in memory.
  • _Release is used with _AddRef to allow an object to track its own lifetime and determine when it is safe to delete itself. Once the reference count reaches zero, the object is freed from memory. Every class that implements interfaces must implement the three IInterface methods, as well as all of the methods declared by any other ancestor interfaces, and all of the methods declared by the interface itself. You can, however, inherit the implementations of methods of interfaces declared in your class.

By implementing these methods yourself, you can provide an alternative means of lifetime management, disabling reference-counting. This is a powerful technique that lets you decouple interfaces from reference-counting.

See Also