Hiding class pointers -- was it a good idea?
Bill Baxter
dnewsgroup at billbaxter.com
Wed Aug 15 10:25:26 PDT 2007
Gregor Richards wrote:
> Bill Baxter wrote:
>> Gregor Richards wrote:
>>> #1 advantage of making them always by-value: Ridiculously
>>> inconsistent use of by-value passing, ref passing and pointer passing.
>>>
>>> void foo(Thing a);
>>> void foo(ref Thing a);
>>> void foo(Thing *a);
>>>
>>> Wait, that's not an advantage at all, that's C++.
>>
>> I don't get your point. Each of those things has different uses in C++.
>>
>> // if Thing is small/PoD and you don't want changes to affect caller
>> void foo(Thing a);
>>
>> // if Thing is bigger, you want to allow caller-visible changes, and
>> you require calling with non-null
>> void foo(ref Thing a);
>>
>> // same as above, but you want to allow null too
>> void foo(Thing *a);
>>
>>
>> The ref-can't-be-null thing may not hold for D, but it's true of C++.
>>
>> --bb
>
> This just makes writing code more complicated. It's difficult to
> remember whether some function wants a reference or a pointer, and you
> have to be careful about how you treat them because they're
> fundamentally different (see the operator overloading post). The
> advantage is virtually nil, and it would create this huge complication.
> That's why it's terrible in C++, and that's why it would be terrible in D.
D functions can also take pointers, values or references, so I don't see
that D is really that different in this respect. I guess you mean that,
*if* you're talking about a class type, then you can be pretty certain
you won't need to call a function using a derefernce like
foo(&theThing). Yeh, I guess that is a positive point of hiding
pointers. Of course it comes at the expense of making it impossible to
pass a class by value. You could also just outlaw pass-by-value for
classes, and automatically dereference any value classes passed to
functions expecting a class pointer. So I don't think it's strictly
necessary to hide the pointer in order to get this benefit you speak of.
--bb
More information about the Digitalmars-d
mailing list