View previous topic :: View next topic |
Author |
Message |
nascent
Joined: 07 Feb 2006 Posts: 26
|
Posted: Thu Feb 09, 2006 1:23 pm Post subject: Private Function |
|
|
I just finnished converting a school assignment from Java to D. And I realized that I was calling a private function from the main function. So my question is why does.
Code: | import std.stdio;
int main() {
TestClass test = new TestClass();
test.privateMethod();
return 0;
}
class TestClass {
private:
void privateMethod() {
writefln("You made it in!");
}
} |
Work? _________________ I am myself and that is me. |
|
Back to top |
|
|
obijohn
Joined: 07 Feb 2006 Posts: 24
|
Posted: Thu Feb 09, 2006 2:55 pm Post subject: |
|
|
because you put them in the same source file.
The private methods of TestClass can be called by any method defined in the same source file.
Put your class definition in a separate file called testme.d, and put "module testme;" at the top of it. Then, at the top of your main file, put "private import testme;" to include it. Now try to compile and you will get an error about the private method not being accessible. |
|
Back to top |
|
|
nascent
Joined: 07 Feb 2006 Posts: 26
|
Posted: Thu Feb 09, 2006 5:29 pm Post subject: |
|
|
Thanks, it isn't imperative that I limit the access (the method was supposed to be public) I was just confused. Coming from Java private only that class can access. _________________ I am myself and that is me. |
|
Back to top |
|
|
Derek Parnell
Joined: 22 Apr 2004 Posts: 408 Location: Melbourne, Australia
|
Posted: Thu Feb 09, 2006 11:05 pm Post subject: Re: Private Function |
|
|
In D, private means private to the module, not private to the class. And a module is the source file. This means that only things in the same source file can access private members. _________________ --
Derek
skype name: derek.j.parnell |
|
Back to top |
|
|
|