Skip to content

Interfaces and multiple inheritance

Now that we've clarified the clue on multiple inheritance, it's worth to take a vary quick look to the wild card we employed: the TInterfacedObject class.

From the FreePascal official documentation (in RTL - RunTime Library section) we see:

type TInterfacedObject = class(TObject, IUnknown)

public

  procedure AfterConstruction; override; //Handle reference count properly.
  procedure BeforeDestruction; override; //Check reference count.
  class function NewInstance; override; //Create a new instance
  property RefCount: LongInt; [r]      //Return the current reference count

end;

TInterfacedObject inherits from the old famous TObject class and from the IUnknown interface.

We note that the goal of it is to manage counters; indeed reference counting is a memorization technique for number of references, pointers or handles of resources like objects or memory blocks.

This suggests to us it involves methods to manage resources during their life-cycle.

On the other hand IUnknown is as follows:

type IUnknown = interface

  function QueryInterface(); //Return pointer to VMT table of interface
  function _AddRef;          //Increase reference count of the interface
  function _Release;         //Decrease reference count of the interface

end;

Its methods (they must be overridden - automatically done) give us a deeper knowledge about the mechanism.

The first function returns a pointer to the Virtual Method Table or VMT of interface.
From the official guide:

VMT is simply a table with pointers to each of the virtual methods: each virtual method has its fixed location in this table (an index). The compiler uses this table to look up the actual method that must be used at runtime. When a descendent object overrides a method, the entry of the parent method is overwritten in the VMT.

By having this clear in mind, the second and third functions are obvious in their goal: they increment and decrement respectively the reference count of interface in the table.

If you want to delve into the matter, here the links where i took information from:

Hope this will be useful, but again... multiple inheritance is an exception other than a rule.