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

Steven Schveighoffer schveiguy at gmail.com
Sat Oct 27 14:20:10 UTC 2018


On 10/26/18 5:30 PM, 12345swordy wrote:
> On Friday, 26 October 2018 at 21:05:56 UTC, Steven Schveighoffer wrote:
>> On 10/26/18 3:28 PM, 12345swordy wrote:
>>> On Friday, 26 October 2018 at 18:57:00 UTC, Neia Neutuladh wrote:
>>>> On Fri, 26 Oct 2018 18:16:38 +0000, 12345swordy wrote:
>>>>> [...]
>>>>
>>>> D's `private` within a module is identical to Java's `private` 
>>>> within a class and its nested classes. The following is valid Java:
>>>>
>>>> [...]
>>>
>>> Again I am referring to classes in the module not the module itself.
>>
>> Classes in the module are in the module. So any classes in the module 
>> *are* the module itself.
>>
>> What it seems like you are saying is that you want non-derivatives to 
>> still be able to access private variables, but derivative classes are 
>> forbidden? Nothing personal, but this really doesn't make a lot of 
>> sense. I feel like maybe there is a misunderstanding.
> 
> There is a misunderstanding! I am asking why the child class have the 
> parent class private member variables in the first place!!!

You mean why are they present in the class instance? That's basic 
inheritance, no?

If that wasn't the case, then you'd have serious issues:

class C
{
    private int x;
    void increment() {++x;}
}

class B : C {}

void main()
{
    auto b = new B;
    b.increment();
}

If b didn't have an `x` in it, then how would this work?

Again, I feel like there is a misunderstanding.

> Do I need to write a DIP for you guys to understand what the hell I am 
> talking about!?

I don't think writing the same words in another place is going to make 
them more understandable.

-Steve


More information about the Digitalmars-d mailing list