Focus

Timon Gehr timon.gehr at gmx.ch
Fri Jan 18 19:22:24 PST 2013


On 01/19/2013 01:04 AM, Andrey wrote:
>>> MyStruct ms;
>>> ms.a = 42; //!!!
>>> writeln(ms.a);
>>
>>
>> This is by design, not a bug. All code in a module has access to all
>> private members in that same module. This obviates the need for the
>> C++ "friend" declarations.
>
> Wikipedia states:
>
> «In general, encapsulation is one of the 4 fundamentals of OOP
> (object-oriented programming). Encapsulation is to hide the variables or
> something inside a class, preventing unauthorized parties to use.»
>

Even if this definition was accurate, note that it does not state what 
"unauthorized" means. Languages that implement OOP can (and do) vary in 
this aspect.

Also, it goes on like:
«So the public methods like getter and setter access it and the other 
classes call these methods for accessing.»

Obviously the author of that sentence had no idea what encapsulation is 
about!

> So how am I supposed to hide the variable inside the struct or class? I
> don't want anything to access it outside struct definition. And I don't
> see any point in giving the opportunity to access it using "friend"
> invitation. I'm sure "friend" explodes the basics of OOP encapsulation
> mechanics.
>

No it does not. "friend" attempts to work around the inexpressiveness of 
accessibility modifiers.

> Struct is an «container» that owns its declarations and use special word
> for this: private. Then comes completely another data structure and can
> easily manipulate private members of another. Only imagine the other man
> from the neighbor house comes into your house and take your children
> without asking only because he lives on the same street.

The module is the house, not the street. "Encapsulation" is sometimes 
applied (or pseudo-applied) at a way too small scale by programmers who 
do not understand it. It is not magic.


More information about the Digitalmars-d mailing list