Why is D unpopular

Mike Parker aldacron at gmail.com
Mon Jun 13 10:13:30 UTC 2022


On Monday, 13 June 2022 at 10:03:59 UTC, Ola Fosheim Grøstad 
wrote:

>
> It wasn't the subclass that tried to access, it was the owner 
> of the subclass, the module.

*Through* the subclass.

> If the owner of Cyborgs
> If a surgeon can operate

I'm a Parker. My father is a Parker. But if you ask me for the 
contents of my father's safe, and I don't have the combination, 
then you aren't getting the contents of my father's safe. Ask me 
to put you in touch with my father though, and you can work 
something out.

It's all about the interface here. An instance of B is an A only 
in terms of the public (and protected) interface. It doesn't have 
access to A's private members if it isn't declared in the same 
module, so you can't get A's private members through an instance 
of B. You have to cast to A.

This doesn't allow access either:

```
module ca;

import cb;

class A {
     private int _x;

     protected void modX(B b) {
         b._x = 10;
     }
}

---

module cb;

import ca;

class B : A {
     void setX() { modX(this); }
}

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

```


More information about the Digitalmars-d mailing list