[fpc-pascal] inherited interfaces not seen by queryinterface / supports
Graeme Geldenhuys
mailinglists at geldenhuys.co.uk
Thu Oct 27 04:47:07 CEST 2016
On 2016-10-27 02:13, David Emerson wrote:
> t_2 = class (_obj_type_, i_2)
> // i_2 inherits from i_1, compiler requires i_1 implementation
> procedure do_one;
> procedure do_two;
> end;
A common misconception about how interfaces work. In fact, I don't
actually know why FPC and Delphi bother with Interface Inheritance,
because I simply don't see the point.
To make your "t_2" class support both interface, you need to specify
both in the class declaration. Even though i_2 inherits from i_1, both
i_1 and i_2 must be specified in the t_2 class.
Again, I don't know why it is done like this, and works very different
to class inheritance. Delphi 7 works exactly the same in this regard.
Maybe somebody with more knowledge on the subject could shed some light.
Here is a D7 example:
=======================================
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
IOne = interface
['{32271879-1C00-4A02-A9C0-6948844028D6}']
procedure One;
end;
ITwo = interface(IOne)
['{F3027177-6638-4984-B842-7D4E70299056}']
procedure Two;
end;
{ If you only specify ITwo here, then the second Supports() call
for i1.One will never execute. }
TMyObject = class(TInterfacedObject, IOne, ITwo)
private
// IOne interface
procedure One;
// ITwo interface
procedure Two;
end;
procedure TMyObject.One;
begin
writeln('One');
end;
procedure TMyObject.Two;
begin
writeln('Two');
end;
var
o: TMyObject;
i1: IOne;
i2: ITwo;
begin
o := TMyObject.Create;
if Supports(o, ITwo, i2) then
i2.Two;
if Supports(o, IOne, i1) then
i1.One;
i2 := nil;
i1 := nil;
end.
=======================================
Regards,
Graeme
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
My public PGP key: http://tinyurl.com/graeme-pgp
More information about the fpc-pascal
mailing list