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

unprotected-entity unprotected-entity at gmail.com
Tue Oct 30 00:01:18 UTC 2018


On Monday, 29 October 2018 at 22:24:22 UTC, Bastiaan Veelo wrote:
>
> I hear you and understand you, but personally I still prefer 
> the D spec as it is in this regard, except for the confusing 
> aspect that you shouldn’t `alias this` a private member; but 
> that is rather a limitation of alias, not of encapsulation.
>

If your were sitting on a plane that was running the following D 
code, you might think differently ;-)

--------
class Plane
{
     private static int MAX_SPEED = 1000;

     public void change_max_speed(int speed)
     {
         if(speed >= 1000)
             MAX_SPEED = speed;
     }

}

immutable Plane p = new Plane();

// god no! why aren't you using the public interface of the class 
for this!
void SetMaxSpeed() { p.MAX_SPEED = -1; }

void Bar() { SetMaxSpeed(); } // mmm...trouble is about to 
begin...

void main()
{
     import std.stdio;

     Bar(); // oohps. thanks D module. we're all going to die!

     // by the time you see this, it's too late for you, and your 
passengers.
     writeln(p.MAX_SPEED);

};


-------



More information about the Digitalmars-d mailing list