Dlang and POO

user1234 user1234 at 12.de
Mon Jun 21 10:16:47 UTC 2021


On Monday, 21 June 2021 at 10:01:03 UTC, christophe__c wrote:
> Hello to all,
>
> I'm starting to program in Dlang. I use for the moment the 
> online editor.
>
> I test the following code:
>
> ```D
> import std;
>
> class User
> {
>
>     private string name;
>
> private:
>     string firstname;
>     int age;
>
> public:
>
>     override string toString()
>     {
>         return (format("User: %s %s - (%s)", this.name, 
> this.firstname, this.age));
>     }
>
> };
>
> void main()
> {
>
>     auto bob = new User();
>     bob.name = "Bob";
>     bob.firstname = "McDean";
>     bob.age = 15;
>
>     writeln(bob);
>
> }
> ```
>
> and I don't understand why I can modify private members from 
> outside my class.
>
> I had understood that it was required to create accessors to 
> have this behavior.
>
> Thanks in advance to all for your welcome and your answers.
>
> Christophe__c

Ah this gain ;)

in D "private" is related to the module scope, not the aggregate 
scope.
So at the module scope you can see private declarations even if 
the "local" scope
is unrelated.


More information about the Digitalmars-d mailing list