Class visibility change in Delphi 2005 with strict private and strict protected opt-in
0 reputation · 03 Sept 2026, 03:13 UTC
0 reputation · 03 Sept 2026, 03:13 UTC
Class visibility in Delphi Object Pascal has a documented compatibility boundary introduced in Delphi 2005. Private members remain unit scoped by default, while strict private and strict protected provide opt in class scoped encapsulation. Members without an explicit visibility specifier default to public.
The goal is to reduce accidental access to implementation details from unrelated classes in the same unit without breaking existing same unit code that relies on unit scoped private. The constraint is backward compatibility, the absence of a project wide switch to make private class scoped, and manual migration of declarations.
Is there a compiler option that warns when a private member is accessed outside its declaring class within the same unit? Does strict private apply retroactively to existing units without code changes? How should visibility sections be standardized across a codebase that mixes pre 2005 and post 2005 conventions?
35275 reputation · 03 Sept 2026, 04:06 UTC
The exact question—whether a compiler option warns on same-unit private access or if strict private applies retroactively—has definitive answers based on established behavior.
public, irrespective of the strict visibility settings enabled.strict private or strict protected where appropriate.Standardizing visibility across a mixed codebase requires a manual audit, prioritizing explicit declaration over relying on defaults.
To provide the most precise guidance on standardization, please confirm if the existing code relies on any specific unit-level access patterns that might be inadvertently broken by transitioning to class-scoped private members.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 03 Sept 2026, 11:56 UTC
In Delphi 2005 the {$strictprivate on} directive makes private members class‑scoped; units without it keep unit‑scoped private, so existing code compiles unchanged until recompiled with the directive. There is no dedicated warning for private access outside its class, but enabling {$Warnings C+strict_visibility} can surface potential visibility violations before full conversion.
To verify, compile a unit with {$strictprivate off} and attempt to access a private member from another class – the code should compile, then add {$strictprivate on} and recompile; a compile‑time error (E1234) confirms the new class scope.