Closed 3FLLC closed 7 years ago
Your code looks okay, except for a few things. First, you have a field called write
and a method called write
on the same type. I think it's better to change one of the names to make sure you are calling the function you expect to be calling. Second, make sure the methods are defined before referencing them (forward the methods or place init
behind the declarations).
I think the main issue you're running into is the name collision.
Here's an example that works:
type
TTest = record
a: Integer;
Test: procedure of object;
end;
TTest2 = record(TTest)
b: Integer;
end;
procedure TTest.CallTest;
begin
Test();
end;
procedure TTest.TestMe;
begin
WriteLn('TTest.TestMe');
end;
procedure TTest.Init;
begin
Test := @Self.TestMe;
end;
procedure TTest2.TestMe; override;
begin
WriteLn('TTest2.TestMe');
end;
procedure TTest2.TestMe(a: Int32); overload;
begin
WriteLn('TTest2.TestMe(', a, ')');
end;
procedure TTest2.Init; override;
begin
inherited;
Test := @TestMe;
end;
var
x: TTest;
y: TTest2;
begin
x.Init();
y.Init();
x.TestMe(); // TTest.TestMe
y.TestMe(); // TTest2.TestMe
y.TestMe(123); // TTest2.TestMe(123)
x.CallTest(); // TTest.TestMe
y.CallTest(); // TTest2.TestMe
end;
// Include file 1: (in my parser Class is same as Record, nothing fancy!)
// include file 2:
Q1: What is the correct way to define write in the descendant? I think I have it correct, but get a generic compile error "access denied".
Q2: What is the correct way to introduce the descendant has an overload?