Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-07

protected members access across classes in the same package, but what about inherited cross package

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Martin Friebe

Just a question, which came to my mind in relation to http://bugs.freepascal.org/view.php?id=11697

I understand that if you declare 2 classes in the same package (same file), then they are allowed to look at each others protected members (variables, properties, func, proc).
I wonder what the intend or language definition says about inherited protected members those classes have, if the parent implementation comes from different files?

Examples:

[file foo.pp]
type
   TThat = class(TObject)
    protected
      ThatValue : integer
   end;

TThis = class(TObject)
....

   end;

implementation ...
[/end of file foo.pp]

I know it is correct that any code of TThis can access ThatValue. If you have code in any other package/file (belonging to any other class) it should not be able to access ThatValue.

[file bar.pp]
type
  TThatFake class(TThat)
  end; // intntionally empty class

TSomething = class(...)
....
[/end of file bar.pp]]

- If TThatFake had any code, then any code belonging to TThatFake would be allowed to see it's anchestor protected members, that would be correct.
- If TThatFake had any protected mebers implemented in bar.pp they should be visble to other code in bar.pp
- But what about inherited members? Should TSomething be able to access ThatValue?

I do not know the answer, it can be argued both ways. But it allows to bypass proper encapsulation as seen in the above link.

Best Regards
Martin

Reply by email

Joao Morais
to Martin Friebe
Martin Friebe wrote 24 lines
[file foo.pp] type TThat = class(TObject) protected ThatValue : integer end; TThis = class(TObject) .... end; implementation ... [/end of file foo.pp] [file bar.pp] type TThatFake class(TThat) end; // intntionally empty class TSomething = class(...) .... [/end of file bar.pp]] - If TThatFake had any code, then any code belonging to TThatFake would be allowed to see it's anchestor protected members, that would be correct.

Yes. Inheritance.

- If TThatFake had any protected mebers implemented in bar.pp they should be visble to other code in bar.pp

Yes. Friend classes.

- But what about inherited members? Should TSomething be able to access ThatValue?

Yes, if the object pointer is declared as TThatFake or you cast the pointer to TThatFake. Otherwise no, only public members of TThat would be visible.

Joao Morais

Reply by email