Recent

Author Topic: Acess to object fields from inherited subclasses: FP bug probably  (Read 3750 times)

HomePlaneR

  • New Member
  • *
  • Posts: 34
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.

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
  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:

Code: [Select]
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:

Code: [Select]
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!

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Acess to object fields from inherited subclasses: FP bug probably
« Reply #1 on: May 04, 2013, 04:19:20 pm »

Code: [Select]
FField := 123;

My question is: which instance of TClass (or TInheritedClass) I access to by this assigning?

You are accessing the class instance you create here, of type TInheritedClass.TInheritedSubclass:
Code: [Select]
var Obj : TInheritedClass.TInheritedSubclass;
begin
 Obj := TInheritedClass.TInheritedSubclass.Create;
 Obj.Bug;
 readln;
end.

Since the Obj instance descends from your (poorly named) TClass, it inherits all its fields and other parts.

HomePlaneR

  • New Member
  • *
  • Posts: 34
Re: Acess to object fields from inherited subclasses: FP bug probably
« Reply #2 on: May 04, 2013, 04:28:08 pm »

Code: [Select]
FField := 123;

My question is: which instance of TClass (or TInheritedClass) I access to by this assigning?

You are accessing the class instance you create here, of type TInheritedClass.TInheritedSubclass:
Code: [Select]
var Obj : TInheritedClass.TInheritedSubclass;
begin
 Obj := TInheritedClass.TInheritedSubclass.Create;
 Obj.Bug;
 readln;
end.
Since the Obj instance descends from your (poorly named) TClass, it inherits all its fields and other parts.

Absolutely not, because Obj has no field FField since it is the instance of TInheritedClass.TInheritedSubclass. It does not descend from TClass but from TSubclass.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Acess to object fields from inherited subclasses: FP bug probably
« Reply #3 on: May 04, 2013, 05:00:32 pm »
I am curious where the FField is stored because no instance of TInheritedClass was ever created.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

HomePlaneR

  • New Member
  • *
  • Posts: 34
Re: Acess to object fields from inherited subclasses: FP bug probably
« Reply #4 on: May 04, 2013, 05:01:25 pm »
I am curious where the FField is stored because no instance of TInheritedClass was ever created.

That's what my question is about. I suppose, it is stored nowhere, like accessing to a field of an unitialized instance. I thought that the compiler should not accept it in this case, like it does not accept the code of TSubClass.NoBug.
« Last Edit: May 04, 2013, 05:12:11 pm by HomePlaneR »

 

TinyPortal © 2005-2018