[fpc-pascal] question with interfaces, hooks
David Emerson
dle3ab at angelbase.com
Wed Jul 15 06:06:09 CEST 2015
Hi list!
I'm just starting in with interfaces, and am happy to see there is
something similar to multiple inheritence available. But it seems to be
limited in a way I will show below, so I am wondering if there is
something I am missing. Example code says it best...
program test_interfaces;
{$mode objfpc}{$H+}
uses classes, sysutils;
type
i_foo = interface
procedure foo;
end;
t_foo_base = class (TInterfacedObject, i_foo)
procedure foo;
end;
t_fancy_class_base = class
// the idea is that I can't reasonably rebase this on t_foo_base
procedure bar;
end;
t_fancy_class = class (t_fancy_class_base, i_foo)
private
f_hook : i_foo;
public
constructor create; // instantiates f_hook as a t_foo_base
destructor destroy; override; // destroys the above
property hook : i_foo read f_hook implements i_foo;
end;
procedure t_foo_base.foo;
begin
writeln ('foo');
end;
procedure t_fancy_class_base.bar;
begin
writeln ('bar');
end;
constructor t_fancy_class.create;
begin
inherited;
f_hook := t_foo_base.create;
end;
destructor t_fancy_class.destroy;
begin
t_foo_base(f_hook).free;
inherited;
end;
var
fc : t_fancy_class;
foo : i_foo;
begin
fc := t_fancy_class.create;
foo := t_fancy_class.create;
fc.foo; // this won't compile-- why?
i_foo(fc).foo; // this works, but is cumbersome
foo.bar; // this understandably won't compile
t_fancy_class(foo).bar; // we can do this if needed
end.
So... since t_fancy_class implements the interface i_foo, why is
t_fancy_class.foo not available? foo is also generally unavailable from
within methods of t_fancy_class, an equal frustration.
Is there some mode switch or some such I can use to make it generally
available?
Thanks!
~David
More information about the fpc-pascal
mailing list