Why are class variables public, when marked by the 'private' keyword?

Kirill kirill.saidov at mail.com
Sat Mar 21 04:45:29 UTC 2020


I was playing around with visibility attributes in D. I created a 
class with private variables. Then I tried to access those 
variables through the class object. It compiled without any 
errors. However, ...

Shouldn't the compiler output an error for trying to access 
private members of a class? Do I get something wrong?

Here is the code:

import std.stdio;

class ID {
public:
      int id = 3849493;
private:
      string name = "Julia";
      int age = 17;
};

void main() {
      ID p = new ID();

      writeln(p.name, " ", p.age, " ", p.id);
}





More information about the Digitalmars-d-learn mailing list