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

Neia Neutuladh neia at ikeran.org
Tue Oct 30 15:12:50 UTC 2018


On Tue, 30 Oct 2018 06:44:10 +0000, unprotected-entity wrote:
> On Tuesday, 30 October 2018 at 01:33:50 UTC, Neia Neutuladh wrote:
>>
>> That your request to make D's `private` work like Java, C#, and C++
>> isn't consistent because they work differently from each other.
>>
>>
> I've already demonstrated that they all work the same:

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).

> btw. here is an interesting paper on 'friends' - well worth reading.

You didn't include a link.

>> When you say "people will expect this to work like these three other
>> languages" and those other languages do that thing differently from
>> each other, it's hard to take your claims seriously.
> 
> really? when I looked at D, I expected the notion of private, declared
> within a class, to be private to that class (no matter what else is in
> the module).
> 
> That is what I expected. This is what C++/c#/Java does. It's a
> reasonable expectation for someone looking at using D. Is it not?

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?

> In D, that overriding happens for you, automatically, within a module,
> and you get no say in it... whatsoever.

Aside from using more modules.

>> I exercise patience at the start, and it wears thin when people
>> repeatedly ignore me.
> 
> You starting to sound like that guy that runs the linux kernel project..
> I believe he's just take time off to deal with that attitude...

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.


More information about the Digitalmars-d mailing list