May 05, 2004
Seems I can access private class members from outside the class?

class Test2
{
private void privateFunct()
{
printf("How can this be?\n");
}
}

int main ( char [] [] args )
{
Test2 t2 = new Test2();
t2.privateFunct();
return 1;
}

This code compiles and runs.  Shouldn't this fail compilation?

Russell


May 05, 2004
Russell wrote:

>Seems I can access private class members from outside the class?
>
>class Test2
>{ private void privateFunct()
>{ printf("How can this be?\n");
>}
>}
>
>int main ( char [] [] args ) {
>Test2 t2 = new Test2();
>t2.privateFunct();
>return 1;
>}
>
>This code compiles and runs.  Shouldn't this fail compilation?
>  
>
The compiler is correct.   Private/Protected only works in module space:

ie
//file X.d
module X;

class Test2
{ private void privateFunct()
{ printf("How can this be?\n");
}
}


//file main

import X;

int main ( char [] [] args ) {
Test2 t2 = new Test2();
t2.privateFunct(); //Compiler Error
return 1;
}


Therefore everything in the same module are friends (to use a C++ term).



-- 
-Anderson: http://badmama.com.au/~anderson/