Why do private member variables behaved like protected in the same module when creating deriving class?
FeepingCreature
feepingcreature at gmail.com
Tue Oct 30 06:45:25 UTC 2018
On Monday, 29 October 2018 at 09:15:35 UTC, unprotected-entity
wrote:
> 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.
You are straight up factually mistaken about how functions work.
void test() { int a; } <- this a does not exist *any* more than
the a in class { int a; } does. It's only actually allocated once
test is called, just like the a in the class is only allocated
once the class is instantiated.
More information about the Digitalmars-d
mailing list