Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 13, 2007 encapsulation | ||||
---|---|---|---|---|
| ||||
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 ???? |
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to arun s | 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.
|
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 |
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 ?????
|
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to arun s | 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.
|
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to arun s | 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 |
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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)
|
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | 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_Really_Windy_City.aspx (at the bottom) If the date thing causes problems I'll do something about it. |
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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_Really_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 |
March 13, 2007 Re: encapsulation | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 */ } --- |
Copyright © 1999-2021 by the D Language Foundation