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

unprotected-entity unprotected-entity at gmail.com
Sun Oct 28 08:36:06 UTC 2018


On Saturday, 27 October 2018 at 16:19:03 UTC, Steven 
Schveighoffer wrote:
>
> This isn't correct. I'd write examples in each of those 
> languages, but don't have the time right now.
>
> -Steve

I have plenty of time ;-)

(in any case, my point is not to make an argument out of this, I 
was stating a fact).

That D does it differently, is fine, if that's how D want to do 
it.

But people coming to D, will be very surprised, and will have to 
come to terms with it, or not.

====== C++ =============

class C
{
   private: int x = 0;
   public: void increment() {++x;}
};

class B : public C {};

int main()
{
     B b;
     b.increment();
     b++; // no way! not in C++

     return 0;
}

===== Java ==============

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

class B extends C {}

public class Program
{
     public static void main(String[] args)
     {
         B b = new B();
         b.increment();
         b++; // seriously. you really think this is going to 
happen? Not in Java it aint.
     }
}

===== C# ==============

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

class B : C {}

public class Program
{
     public static int Main()
     {
         B b = new B();
         b.increment();
         b++; // forget it. It's not going to happen. Not in C#

         return 0;
     }
}

===== D ========
module test;

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

class B : C {}

void main()
{
     B b = new B;
     b.increment();
     b.x++; // sure, no problem. I'll do that for you, cause I'm D.
}

===== THE END =======



More information about the Digitalmars-d mailing list