Recent

Author Topic: [SOLVED] Able to access private member  (Read 220 times)

vinntec

  • New Member
  • *
  • Posts: 11
[SOLVED] Able to access private member
« on: January 12, 2026, 09:09:29 am »
I must have done something stupid, but I can't see what. I am following a book "The Modern Day Pascal Developer" as I get back into using Pascal (used UCSD Pascal and Turbo Pascal in the past). One of the examples is to demonstrate encapsulation which demonstrates that you cannot access a private class member from outside the class. However, if I uncomment the line to access the private member FValue from main and add a second list, the private member is being changed. What have I done wrong?
Code: Pascal  [Select][+][-]
  1. program EncapsulationDemo;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. type
  6.   TCounter = class
  7.   private
  8.     FValue: Integer;  { hidden }
  9.   public
  10.     procedure Increment;
  11.     procedure Reset;
  12.     procedure Show;
  13.   end;
  14.  
  15. procedure TCounter.Increment;
  16. begin
  17.   Inc(FValue);
  18. end;
  19.  
  20. procedure TCounter.Reset;
  21. begin
  22.   FValue := 0;
  23. end;
  24.  
  25. procedure TCounter.Show;
  26. begin
  27.   Writeln('Counter = ', FValue);
  28. end;
  29.  
  30. var
  31.   C: TCounter;
  32.  
  33. begin
  34.   C := TCounter.Create;
  35.   C.Reset;
  36.   C.Increment;
  37.   C.Increment;
  38.   C.Show;        { should print 2 }
  39.   C.FValue := 100;  // not allowed: FValue is private
  40.   C.Show;        { should print 2 }
  41.   C.Free;
  42.   Writeln('Press Enter to exit...');
  43.   Readln;
  44. end.
Code: Pascal  [Select][+][-]
  1. Counter = 2
  2. Counter = 100
  3. Press Enter to exit...
  4.  
« Last Edit: January 12, 2026, 09:30:27 am by vinntec »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: Able to access private member
« Reply #1 on: January 12, 2026, 09:17:16 am »
-----8<-----
 Private
    All fields and methods that are in a private block, can only be accessed in the module (i. e. unit) that contains the class definition. They can be accessed from inside the classes’ methods or from outside them (e. g. from other classes’ methods)
Strict Private
    All fields and methods that are in a strict private block, can only be accessed from methods of the class itself. Other classes or descendent classes (even in the same unit) cannot access strict private members.
----->8-----

https://www.freepascal.org/docs-html/current/ref/refse35.html#x70-940006.1

For that reason I normally use  strict private  throughout.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Nimbus

  • Jr. Member
  • **
  • Posts: 84
Re: Able to access private member
« Reply #2 on: January 12, 2026, 09:18:22 am »

vinntec

  • New Member
  • *
  • Posts: 11
Re: Able to access private member
« Reply #3 on: January 12, 2026, 09:27:53 am »
Thank you guys - that explains it. If I move the class into a unit by itself then private works as expected. But now I know that private means I can access from the same unit (single file in this case) so need to use strict private in those cases.

 

TinyPortal © 2005-2018