The problem with const (and a solution)

Steven Schveighoffer schveiguy at yahoo.com
Fri Dec 7 10:15:45 PST 2007


"Sönke Ludwig" wrote
> Steven Schveighoffer schrieb:
>>>
>>> // struct references
>>> S a = S(1);
>>> S& b = a;
>>
>> This would not compile, a is a value type, b is a pointer type, you need:
>> S&b = &a;
>
> Hm, ok, so S& is the same as S* then?

Yes

>
> The class example should just show that it might be unexpected for a C++ 
> programmer that the assert fails. But then again, D's classes already 
> behave differently anyway.

Yeah, I was intending the X& meaning to be 'pointer to value', not for it to 
mean 'C++ reference'.  This works for D because d's pointer to values 
already act like C++ references when using the object, but act like pointers 
when assigning.

For example:

S s1;
S* sp = new S;
sp = &s1; // acts like a pointer
s.x = 5; // acts like a reference, sets s1.x

for classes, there is an implied pointer, which is what makes this 
tail-const problem so hard...

-Steve 





More information about the Digitalmars-d mailing list