May 05, 2004 Private member access | ||||
---|---|---|---|---|
| ||||
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 Re: Private member access | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell | 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/ |
Copyright © 1999-2021 by the D Language Foundation