www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - encapsulation

reply arun s <1986.arun gmail.com> writes:
class A { 
     private int c=0;  
}


void main() {
      A a = new A();
      printf("%d",a.c);
}

consider the above code snippet..... y there is no error in printf statement 
a.c , since attribute "c" is private ???? 
Mar 13 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to arun,

 class A {
 private int c=0;
 }
 void main() {
 A a = new A();
 printf("%d",a.c);
 }
 consider the above code snippet..... y there is no error in printf
 statement a.c , since attribute "c" is private ????
 
Everything inside of a module (a.k.a. file) is visible to everything else inside of that module.
Mar 13 2007
next sibling parent Lars Ivar Igesund <larsivar igesund.net> writes:
BCS wrote:

 Reply to arun,
 
 class A {
 private int c=0;
 }
 void main() {
 A a = new A();
 printf("%d",a.c);
 }
 consider the above code snippet..... y there is no error in printf
 statement a.c , since attribute "c" is private ????
 
Everything inside of a module (a.k.a. file) is visible to everything else inside of that module.
It's a variation on C++ friends. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango
Mar 13 2007
prev sibling parent reply arun s <1986.arun gmail.com> writes:
BCS Wrote:

 Reply to arun,
 
 class A {
 private int c=0;
 }
 void main() {
 A a = new A();
 printf("%d",a.c);
 }
 consider the above code snippet..... y there is no error in printf
 statement a.c , since attribute "c" is private ????
 
Everything inside of a module (a.k.a. file) is visible to everything else inside of that module.
how is the concept of data hiding and encapsulation implemented in D ?????
Mar 13 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to arun,

 BCS Wrote:
 
 Reply to arun,
 
 class A {
 private int c=0;
 }
 void main() {
 A a = new A();
 printf("%d",a.c);
 }
 consider the above code snippet..... y there is no error in printf
 statement a.c , since attribute "c" is private ????
Everything inside of a module (a.k.a. file) is visible to everything else inside of that module.
how is the concept of data hiding and encapsulation implemented in D ?????
If I understand correctly, the idea is to get rid of friend but still let some things look inside of others. This solution lets closed set of code interact at a low level while doing encapsulation at wider scopes. The reason friend is discarded is that it has the power to do arbitrary snooping which causes the same kind of problems as goto's arbitrary redirection causes.
Mar 13 2007
next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
BCS wrote:
 [stuff]
with headers:
 Date: Tue, 13 Mar 2007 18:48:08 +0000 (UTC)
 X-Trace: [more numbers] (13 Mar 2007 17:48:15 GMT)
 NNTP-Posting-Date: Tue, 13 Mar 2007 17:48:15 +0000 (UTC)
The clock on your computer seems to be an hour ahead... (Or you've already switched to Daylight Savings and forgot to set the right timezone your computer so it can correct accordingly when converting to UTC)
Mar 13 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Frits,

 BCS wrote:
 
 [stuff]
 
with headers:
 Date: Tue, 13 Mar 2007 18:48:08 +0000 (UTC) X-Trace: [more numbers]
 (13 Mar 2007 17:48:15 GMT) NNTP-Posting-Date: Tue, 13 Mar 2007
 17:48:15 +0000 (UTC)
 
The clock on your computer seems to be an hour ahead... (Or you've already switched to Daylight Savings and forgot to set the right timezone your computer so it can correct accordingly when converting to UTC)
Actually I have the correct time and time zone but I haven't patched my system to be in sync with the new Daylight savings time. Last time I patched "Just because I can" I sent in the result to the dailyWTF <g> http://thedailywtf.com/Articles/Pop-up_Potpourri_0x3a__The_Re lly_Windy_City.aspx (at the bottom) If the date thing causes problems I'll do something about it.
Mar 13 2007
parent Pragma <ericanderton yahoo.removeme.com> writes:
BCS wrote:
 Reply to Frits,
 
 BCS wrote:

 [stuff]
with headers:
 Date: Tue, 13 Mar 2007 18:48:08 +0000 (UTC) X-Trace: [more numbers]
 (13 Mar 2007 17:48:15 GMT) NNTP-Posting-Date: Tue, 13 Mar 2007
 17:48:15 +0000 (UTC)
The clock on your computer seems to be an hour ahead... (Or you've already switched to Daylight Savings and forgot to set the right timezone your computer so it can correct accordingly when converting to UTC)
Actually I have the correct time and time zone but I haven't patched my system to be in sync with the new Daylight savings time. Last time I patched "Just because I can" I sent in the result to the dailyWTF <g> http://thedailywtf.com/Articles/Pop-up_Potpourri_0x3a__The_Re lly_Windy_City.aspx (at the bottom) If the date thing causes problems I'll do something about it.
Oh, so that *was* you. Kudos for getting your submission in on one of the funnier WTF Potpourri's in a while. :) And yes, thank you for pressing Cancel. -- - EricAnderton at yahoo
Mar 13 2007
prev sibling parent reply torhu <fake address.dude> writes:
BCS wrote:
 If I understand correctly, the idea is to get rid of friend but still let 
 some things look inside of others. This solution lets closed set of code 
 interact at a low level while doing encapsulation at wider scopes. The reason 
 friend is discarded is that it has the power to do arbitrary snooping which 
 causes the same kind of problems as goto's arbitrary redirection causes.
'friend' doesn't work they way you're implying here. From Stroustrup himself, at http://www.research.att.com/~bs/bs_faq2.html#friend --- Does "friend" violate encapsulation? No. It does not. "Friend" is an explicit mechanism for granting access, just like membership. You cannot (in a standard conforming program) grant yourself access to a class without modifying its source. For example: class X { int i; public: void m(); // grant X::m() access friend void f(X&); // grant f(X&) access // ... }; void X::m() { i++; /* X::m() can access X::i */ } void f(X& x) { x.i++; /* f(X&) can access X::i */ } ---
Mar 13 2007
parent BCS <ao pathlink.com> writes:
Reply to torhu,

 BCS wrote:
 
 If I understand correctly, the idea is to get rid of friend but still
 let some things look inside of others. This solution lets closed set
 of code interact at a low level while doing encapsulation at wider
 scopes. The reason friend is discarded is that it has the power to do
 arbitrary snooping which causes the same kind of problems as goto's
 arbitrary redirection causes.
 
'friend' doesn't work they way you're implying here.
Yes, friend acts as an invitation, however there are no limitations on who a class can invite. This is "arbitrary snooping" I was talking about. With D's private semantics, that same "you can look at my private stuff" ability is limited to a finite set of code. It is somewhat like how goto can go to most anywhere in a function but labeled break and continues can only go to the start or end of a loop.
Mar 13 2007
prev sibling parent Pragma <ericanderton yahoo.removeme.com> writes:
arun s wrote:
 BCS Wrote:
 
 Reply to arun,

 class A {
 private int c=0;
 }
 void main() {
 A a = new A();
 printf("%d",a.c);
 }
 consider the above code snippet..... y there is no error in printf
 statement a.c , since attribute "c" is private ????
Everything inside of a module (a.k.a. file) is visible to everything else inside of that module.
how is the concept of data hiding and encapsulation implemented in D ?????
Simple. :) Use separate modules/files: //test.d class A{ private int c = 0; } //main.d import test; void main(){ A a = new A(); printf("%d",a.c); }
 dmd main test
 main.d(5): class test3.A member c is not accessible
You're correct about using 'private' or even 'protected' for encapsulation. As others have stated, keeping everything in one file *implicitly* side-steps those rules. It's a very useful feature if exploited correctly. -- - EricAnderton at yahoo
Mar 13 2007
prev sibling parent reply "David B. Held" <dheld codelogicconsulting.com> writes:
arun s wrote:
 class A { 
      private int c=0;  
 }
 
 
 void main() {
       A a = new A();
       printf("%d",a.c);
 }
 
 consider the above code snippet..... y there is no error in printf statement 
 a.c , since attribute "c" is private ???? 
In D, 'private' really means 'package private', like Java's 'package' visibility (or 'default' visibility), not like C++'s 'private'. While I prefer tight visibility semantics, I think this is a reasonable compromise. Dave
Mar 22 2007
parent Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
David B. Held wrote:
 arun s wrote:
 class A {      private int c=0;  }


 void main() {
       A a = new A();
       printf("%d",a.c);
 }

 consider the above code snippet..... y there is no error in printf 
 statement a.c , since attribute "c" is private ???? 
In D, 'private' really means 'package private', like Java's 'package' visibility (or 'default' visibility), not like C++'s 'private'. While I prefer tight visibility semantics, I think this is a reasonable compromise. Dave
Actually its module-private, with the seperate 'package' attribute providing package-level visibility. Unfortunately 'package' currently suffers in that such declerations are hidden from sub-packages. -- Chris Nicholson-Sauls
Mar 22 2007