Why do private member variables behaved like protected in the same module when creating deriving class?

unprotected-entity unprotected-entity at gmail.com
Mon Oct 29 09:15:35 UTC 2018


On Monday, 29 October 2018 at 08:34:39 UTC, FeepingCreature wrote:
> Yes they do.
> ---
> module test;
> import std.stdio;
> class C { int a; }
> void main() { writeln(a); } // Error: undefined identifier 'a'

Sorry, but you are wrong.

Why? Because 'a' is nothing - it does not even exist. Not 
anywhere.

To make it exist, you need to instantiate an object of C.

Once it actually 'exists', you can do whatever you like with it, 
in the module, regardless of its access modifier. That means, 
quite simply, that the class is not a unit of encapsulation - 
within the module. Outside the module it is, but inside, it is 
not.

That is just fact. It's not something we should be arguing about 
- cause it's just a fact.

On the otherhand, a function (unlike a class) within a module, 
still retains it's encapsulating properties.

i.e.
void test() {int a;};
void main() {a++;}; // Error: undefined identifier 'a' - but this 
time, 'a' does exist - it's just that its correctly encapsulated 
in the function.


Just imagine if a function did not have those encapsulating 
properties.

Now, just imagine if a class did not (well, you don't need to 
imagine it ;-)

I'm not trying to argue here. I'm just trying to clarify a fact, 
that some seem unable, or unwilling, to accept.




More information about the Digitalmars-d mailing list