Recent

Author Topic: Nested Classes and Scope  (Read 2952 times)

syntonica

  • Full Member
  • ***
  • Posts: 120
Nested Classes and Scope
« on: December 20, 2019, 11:49:32 pm »
I'm having an argument with nested classes. They're not working how I'm telling them to work, and I pay my classes well!  :P

Here is my example:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. program test6;
  3.  
  4. type
  5.   Class1 = class
  6.     type
  7.       Class2 = class
  8.         procedure find;
  9.       end;
  10.     var
  11.       b: Class2;
  12.       ICanSeeYou: Boolean;
  13.   end;
  14.  
  15. procedure Class1.Class2.find;
  16.   begin
  17.     if ICanSeeYou then WriteLn('Found you!') else WriteLn('Where are you?');  //Fails compilation!
  18.   end;
  19.  
  20. var
  21.   a: Class1;
  22.  
  23. begin
  24.   a := Class1.Create;
  25.   a.ICanSeeYou := True;
  26.   a.b := a.Class2.Create;
  27.   a.b.find;
  28. end.
  29.  

Currently "ICanSeeYou" is not in Class2's scope. Is there any way to solve this other than passing my Class1 instance explicitly?  Making 'ICanSeeYou' static is not an option.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Nested Classes and Scope
« Reply #1 on: December 20, 2019, 11:53:08 pm »
No. Inner classes and their parents work like any other two classes in this regard.

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Nested Classes and Scope
« Reply #2 on: December 21, 2019, 12:22:39 am »
Meh. No point in nesting then...


Looks like I'll have to pass the pointer.  :'(


Thanks!

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Nested Classes and Scope
« Reply #3 on: December 21, 2019, 01:54:52 am »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11. Class1 = Class
  12.    Type
  13.     Class2 = Class
  14.      fOwner :Tobject;
  15.      Procedure Find;
  16.      constructor Create(aOwner:Tobject);
  17.     End;
  18.   Var
  19.   IcanSeeYou:Boolean;
  20.   B:Class2;
  21. end;
  22.  
  23.   { TForm1 }
  24.  
  25.   TForm1 = class(TForm)
  26.     Button1: TButton;
  27.     procedure Button1Click(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.  
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.Button1Click(Sender: TObject);
  44. Var
  45.   B:Class1;
  46. begin
  47.   B := Class1.Create;
  48.   B.B := B.Class2.Create(B);
  49.   B.IcanSeeYou := True;
  50.   B.B.Find;
  51.   B.B.Free;
  52.   B.Free;
  53. end;
  54. Constructor Class1.Class2.Create(AOwner:Tobject);
  55. Begin
  56.   fOwner := AOwner;
  57. end;
  58.  
  59. Procedure Class1.Class2.Find;
  60. Begin
  61.   If Class1(fOwner).ICanSeeYou  Then Form1.Caption := 'Yup';
  62. End;
  63.  
  64. end.
  65.  

Why someone would want to do this is beyond me but what ever! :D
The only true wisdom is knowing you know nothing

syntonica

  • Full Member
  • ***
  • Posts: 120
Re: Nested Classes and Scope
« Reply #4 on: December 21, 2019, 03:21:55 am »
Code: Pascal  [Select][+][-]
  1. Totally obvious answer.
  2.  

Why someone would want to do this is beyond me but what ever! :D
Duh. I've been Pascalizing way too much these last two weeks...  %)

I needed a set of classes that had access to the main class's stuff. I thought there would be a some love between them, but looks like classes are their own little fiefdoms.

Anyway, y'all are most helpful, as always! Thank you!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Nested Classes and Scope
« Reply #5 on: December 21, 2019, 04:37:03 pm »
Meh. No point in nesting then...
Looks like I'll have to pass the pointer.  :'(

Not much point indeed. Only for generics there is some use (since then the nested type specializes with the mother type), otherwise I never use nested classes.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Nested Classes and Scope
« Reply #6 on: December 21, 2019, 06:11:48 pm »
maybe a advanced record or Object inside would be more appropriate since then one would not need to worry about freeing it.
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11947
  • FPC developer.
Re: Nested Classes and Scope
« Reply #7 on: December 21, 2019, 06:38:19 pm »
maybe a advanced record or Object inside would be more appropriate since then one would not need to worry about freeing it.

Usually the nested types in those generics are record based iterators, yes.

Weiss

  • Full Member
  • ***
  • Posts: 187
Re: Nested Classes and Scope
« Reply #8 on: November 13, 2024, 08:46:36 pm »
good example above by Jamie. One thing I am still trying to get is fOwner, why do we need it, can another object claim ownership of another object's nested object? I am using word "object" as instance of class.

Warfley

  • Hero Member
  • *****
  • Posts: 1763
Re: Nested Classes and Scope
« Reply #9 on: November 13, 2024, 09:57:45 pm »
You have access to the symbols of your parent class, but not of the instance:
Code: Pascal  [Select][+][-]
  1. type
  2.   TParent = class
  3.   public type
  4.     TChild = class
  5.     public procedure Foo;
  6.     end;
  7.   public
  8.     class procedure Bar;
  9.   end;
  10.  
  11. procedure TParent.TChild.Foo;
  12. begin
  13.   Bar; // Symbol of parent class
  14. end;
  15.  
  16. class procedure TParent.Bar;
  17. begin
  18.   WriteLn('Bar');
  19. end;

Because the child type is a member of the class, not of the instance (if that makes sense) you only have access to class symbols

 

TinyPortal © 2005-2018