Inheritance problem
Steven Schveighoffer
schveiguy at yahoo.com
Mon Feb 14 08:13:09 PST 2011
On Mon, 14 Feb 2011 10:52:41 -0500, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> On 2/14/11, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>> What's wrong with:
>>
>> class Foo {
>> private int _x, _y;
>> this(int x, int y) {
>> _x = x;
>> _y = y;
>> }
>> int sumXY() {
>> return _x + _y;
>> }
>> }
>
> Nothing! But the OP asked why it's possible to access the private
> state of a class if the code that accesses it is in the same module as
> the class. So I just threw in an example (which admittedly doesn't
> make much sense, and personally I don't like to access private state
> outside the class).
I meant as an alternative to UFC syntax. I still am not enamored by it's
capabilities.
FWIW, classes/functions/structs in the same module are able to access all
private and protected data to alleviate the need for the 'friend'
attribute in C++. In C++ a friend function is able to access all private
data. The most common use of friend functions is for output stream
processors (since the operator is on the stream and not the object being
outputted).
The theory for the private access in a module goes that likely the same
author wrote all the code in that module, so he should understand what the
code is supposed to do and have free access to anything. It seems to work
pretty well in practice (at least for me anyway).
-Steve
More information about the Digitalmars-d-learn
mailing list