[fpc-devel] Circular references and forward declarations
Nikolai ZHUBR
zhubr at mail.ru
Wed Jan 6 02:08:35 CET 2010
Tuesday, January 05, 2010, 11:08:37 PM, Juha Manninen wrote:
> On tiistai, 5. tammikuuta 2010 20:06:42 Florian Klaempfl wrote:
>> Then do the same as in C++ and put it in different include files.
> Right, include files could solve this problem at least partly. They seem to be
Curious enough, nobody even mentioned interfaced objects (interfaces),
though they are available in both FPC and Delphi since ages.
Not that I would recommend this seriously, however here goes:
//---------------------------------------------------
unit ihelper;
interface
type
IMyClass1 = Interface;
IMyClass2 = Interface;
IMyClass1 = Interface
function SomethingUsefull1: byte;
end;
IMyClass2 = Interface
function SomethingMuchMoreUsefull2: longint;
end;
implementation
end.
//---------------------------------------------------
unit myunit1;
interface
uses
classes, ihelper;
type
Tmyclass1 = class(TInterfacedObject, IMyClass1)
public
Fmyclass2: IMyClass2;
FSomethingUsefull1: byte;
function SomethingUsefull1: byte;
function CrossFunc1: longint;
end;
implementation
function Tmyclass1.SomethingUsefull1: byte;
begin
result := FSomethingUsefull1;
end;
function Tmyclass1.CrossFunc1: longint;
begin
result := Fmyclass2.SomethingMuchMoreUsefull2;
end;
end.
//---------------------------------------------------
unit myunit2;
interface
uses
classes, ihelper;
type
Tmyclass2 = class(TInterfacedObject, IMyClass2)
public
Fmyclass1: IMyClass1;
FSomethingMuchMoreUsefull2: longint;
function SomethingMuchMoreUsefull2: longint;
function CrossFunc2: byte;
end;
implementation
function Tmyclass2.SomethingMuchMoreUsefull2: longint;
begin
result := FSomethingMuchMoreUsefull2;
end;
function Tmyclass2.CrossFunc2: byte;
begin
result := Fmyclass1.SomethingUsefull1;
end;
end.
//---------------------------------------------------
program circexamp;
uses
myunit1, myunit2;
var
c1: Tmyclass1;
c2: Tmyclass2;
begin
c1 := Tmyclass1.Create;
c2 := Tmyclass2.Create;
c1.Fmyclass2 := c2;
c2.Fmyclass1 := c1;
c1.FSomethingUsefull1 := 1;
c2.FSomethingMuchMoreUsefull2 := 2;
writeln(c1.CrossFunc1);
writeln(c2.CrossFunc2);
end.
//---------------------------------------------------
More information about the fpc-devel
mailing list