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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Oct 26 23:03:07 UTC 2018


On Friday, October 26, 2018 3:30:25 PM MDT 12345swordy via Digitalmars-d 
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!!!
> Do I need to write a DIP for you guys to understand what the hell
> I am talking about!?

Derived classes _always_ have the private members of their base classes,
because the base classes are part of the derived classes. Derived classes
extend the base classes and literally have the base class members inside of
them and wouldn't work properly if they didn't. However, if they're in
separate modules, then the derived class does not have access to them.
They're there but inaccessible. That would be the same in C++, Java, C#,
etc. The difference is that in D, if you then put them in the same module,
the derived class then has access to the base class' member variables,
because private is private to the module, not the class. You'd get the same
in C++ if you made them friends, but you'd have to be explicit about it,
since friends require you to be explicit about it.

- Jonathan M Davis





More information about the Digitalmars-d mailing list