www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Private member access

reply Russell <Russell_member pathlink.com> writes:
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
parent J Anderson <REMOVEanderson badmama.com.au> writes:
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/
May 05 2004