const again
Walter Bright
newshound1 at digitalmars.com
Thu Dec 6 21:32:03 PST 2007
Steven Schveighoffer wrote:
> "Walter Bright" wrote
>> Steven Schveighoffer wrote:
>>> What happens if you have this?
>>> const(T)*[]
>> Array of pointers to constant T.
>>
>>> If T is a struct, then it's fine, but if it's a class, then what? To me
>>> the situation is just as bad.
>> It is perfectly consistent. I don't see why it is bad.
>
> const(T)*[] x;
>
> x[0].member is a struct member if T is a struct, what is it if T is a class?
> I was under the (possibly wrong) impression that this is illegal?
I don't understand. Exactly what is illegal?
> Sure, but your original point was to be "able to wrap any type with a
> struct, and have it be possible for that
> struct to behave as if it were that member type". I believed you were
> implying that this was a reason NOT to have a & operator. My question to
> you is, how does having a reference operator make this more difficult than
> not having one? A reference is a pointer.
There's the problem we're having. While a class reference is implemented
as a pointer 'under the hood', to the type system, it is not a pointer.
> OK, I am confused, isn't const(S)* tail const?
No. For const(int*), tail const of that construct would mean that the
pointer is mutable while what it points to is not.
> And if so, how does this new
> scheme rid us of tail const?
It would say that const(int*) would mean an immutable pointer to an
immutable int.
> Couldn't I use this to call a const member
> function on a struct?
I'm very confused as to what you're referring to.
> The way I am interpreting your new const is that I can have tail-const for
> struct references (pointers) but not tail-const for class references.
No. The correct interpretation is that all parts of the type within the
( ) are immutable.
> In other words, I can create a tail-const struct array via:
>
> const(S)*[] tailconst;
> tailconst[0] = new S; // ok to assign to tailconst[0]
> tailconst[0].nonconstfunc; // error, cannot call const function
>
> How do I do the same for a class?
You cannot separate the class reference from the class fields.
More information about the Digitalmars-d
mailing list