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

aliak something at something.com
Tue Oct 30 16:39:31 UTC 2018


On Tuesday, 30 October 2018 at 15:12:50 UTC, Neia Neutuladh wrote:
> On Tue, 30 Oct 2018 06:44:10 +0000, unprotected-entity wrote:
>> [...]
>
> I just pointed out ways in which they don't work the same. 
> Let's add in some code to help out:
>
> C++:
> ---
> class C
> {
>   private int i;
>   friend void foo();
> };
> void foo()
> {
>   C c;
>   c.i = 10;
> }
> ---
>
> Java and C# do not have a way to declare friends.
>
> Also C++:
> ---
> class C {
>   class CA {
>     void foo() {
>       CB b;
>       // access error
>       b.j++;
>       C c;
>       // works fine
>       c.i++;
>     }
>   };
>   class CB {
>     private: int j;
>   };
>   private: int i;
> };
> ---
>
> In C++, you can access private variables in your containing 
> class (and its retaining class recursively), but not in other 
> classes it contains.
>
> Java:
> ---
> public class C {
>   private int i;
>   public class Nested {
>     private int j;
>   }
>   public class Nested2 {
>     public void foo(Nested n, C c) {
>       n.j++;
>       c.i++;
>     }
>   }
> }
> ---
>
> In Java, protections apply to the outermost nested class. You 
> can access anything that any enclosing or nested class can 
> access.
>
> C# doesn't let you do any of this. In C#, you can make 
> something private to a DLL, or you can make it private to 
> exactly one type (and none of its nested types or parent types).
>
>> [...]
>
> You didn't include a link.
>
>> [...]
>
> Again, C++, C#, and Java all do different things with 
> protection attributes, so saying you want something that 
> satisfies people's expectations when they're coming from C++, 
> C#, or Java doesn't give an actual objective.
>
> I also did a survey of like 14 different programming languages 
> to see what they did, and there is very little consistency. So 
> why do you want C++, C#, and Java users to have an easier time 
> with D's protection attributes instead of people coming from 
> some other set of languages?
>
>> [...]
>
> Aside from using more modules.
>
>> [...]
>
> If you want people to change how they communicate with you, it 
> might benefit you to consider how to accomplish that. Trying to 
> call someone an asshole on the sly doesn't seem like an 
> effective way to improve their attitudes toward you.

He's talking about private in general meaning that something is 
accessible within the scope it's declared in. Which "in general" 
is what private means, obviously some languages have caveats, and 
some languages behave differently. C++, C#, Swift, and Java, 
Kotlin, all use private in a class to mean "this variable is only 
accessible from the scope of this class" where the definition of 
scope varies (as you have pointed out) between languages. And 
that  is quite consistent. Rust doesn't have a keyword called 
private AFAIK.

Cheers,
- Ali


More information about the Digitalmars-d mailing list