FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Private Function

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
nascent



Joined: 07 Feb 2006
Posts: 26

PostPosted: Thu Feb 09, 2006 1:23 pm    Post subject: Private Function Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
obijohn



Joined: 07 Feb 2006
Posts: 24

PostPosted: Thu Feb 09, 2006 2:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
nascent



Joined: 07 Feb 2006
Posts: 26

PostPosted: Thu Feb 09, 2006 5:29 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Derek Parnell



Joined: 22 Apr 2004
Posts: 408
Location: Melbourne, Australia

PostPosted: Thu Feb 09, 2006 11:05 pm    Post subject: Re: Private Function Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group