Hi there.
I would like to get some expert opinion about a strange behavior of FP compiler that I have just discovered. Maybe it is a bug, but I didn't report yet because I'm not sure.
Consider the following simple example. I have a class TClass and a subclass TClass.TSubclass defined in it. Class TClass has a field FField as well.
type
TClass = class
private type
TSubclass = class
procedure NoBug;
end;
public
FField : integer;
end;
In TSubClass.NoBug procedure I try to do the following simple thing:
FField:=123;
This is not compilable: I get "Error: Only class methods, class properties and class variables can be referred with class references", which is OK, because I do not precise which instance of TClass I try access to.
Then, I derive TInheritedClass and TInheritedClass.TInheritedSubclass as follows:
TInheritedClass = class (TClass)
public type
TInheritedSubclass = class (TSubclass)
procedure Bug;
end;
end;
This time, in TSubClass.Bug procedure I try to do exactly the same thing:
FField := 123;
And this time the code is compilable.
My question is the same: which instance of TClass (or TInheritedClass) I access to by this assigning?
It even works (the application does not crash). However, I do believe that in a more complicated case (when I'm trying to access to some more complex instance data than just an integer) it will cause the application to crash, because in my project code I had strange crashes with indistinct description that disappeared after I fixed this kind of things in my code. I do not succeed to reproduce this crash with this simple example.
Lazarus 1.1 / SVN 40201
FPC 2.6.1
Completed example code:
program project1;
{$APPTYPE CONSOLE}
type
{ TClass }
TClass = class
private type
{ TSubclass }
TSubclass = class
procedure NoBug;
end;
public
FField : integer;
end;
{ TInheritedClass }
TInheritedClass = class (TClass)
public type
{ TInheritedSubclass }
TInheritedSubclass = class (TSubclass)
procedure Bug;
end;
end;
{ TClass.TSubclass }
procedure TClass.TSubclass.NoBug;
begin
//FField:=0; "Error: Only class methods, class properties and class variables can be referred with class references"
//It is OK that I got this error, because I didn't specify TClass instance, right?
end;
{ TInheritedClass.TInheritedSubclass }
procedure TInheritedClass.TInheritedSubclass.Bug;
begin
FField:= 123; //compilable!
end;
var Obj : TInheritedClass.TInheritedSubclass;
begin
Obj := TInheritedClass.TInheritedSubclass.Create;
Obj.Bug;
readln;
end.
Many thanks in advance!