Recent

Author Topic: Why is a private type of a Class is visible through property of that class?  (Read 1072 times)

Valdas

  • New Member
  • *
  • Posts: 49
Hello,

in Unit2 is declared:
Code: Pascal  [Select][+][-]
  1.     TTest_class = class(TCustomControl)
  2.     private type
  3.         TChoise = (tcc__One, tcc__Two);  // <- private !!
  4.     private
  5.         FChoise: TChoise;
  6.         procedure Set__Choise(verte: TChoise);
  7.     public
  8.         property Choise: TChoise read FChoise write Set__Choise;
  9.     end;
  10.  

Unit1 uses Unit2, and in Unit1 has a procedure:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   obj: TTest_class;
  4. begin
  5.     obj := TTest_class.Create(self);
  6.  
  7.     obj.Choise := tcc__Two; // <- accessing private type
  8.  
  9.     FreeAndNil(obj);
  10. end;
  11.  

The Compiler does not issue any warnings and the project compiles fine.

Public property makes private type public?

« Last Edit: June 09, 2023, 12:21:34 pm by Valdas »

TRon

  • Hero Member
  • *****
  • Posts: 2435
That is called scoped enums (or lack thereof), see https://www.freepascal.org/docs-html/prog/progsu70.html

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
make the private sections strict private? Although the documentation does not mention types, only methods and fields so you must assume that the type is always public.
« Last Edit: June 09, 2023, 03:33:25 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
The Compiler does not issue any warnings and the project compiles fine.

As TRon remarked, by default the enum values are declared both locally and globally. In your example tcc__One is visible both as UnitName.tcc__One and UnitName.TTest.TChoise.tcc__One with the later only being usable inside of TTest_class (and - as it is not declared as strict private - inside the same unit). With the {$ScopedEnums On} directive you can disable the generation of the global symbol, which means that you must use TChoise.tcc__One then.

 

TinyPortal © 2005-2018