The problem with const (and a solution)
Steven Schveighoffer
schveiguy at yahoo.com
Fri Dec 7 09:27:54 PST 2007
"Sönke Ludwig" <ludwig at informatik_dot_uni-luebeck.de> wrote in message
news:fjbtgu$24dm$1 at digitalmars.com...
> Steven Schveighoffer wrote:
>> const(*C)* c;
>>
>
> This would be a workable solution. Of course not pretty, but workable.
>
>> C& c; // reference to a C class
>> S& s; // reference to a S struct
>
> I like the idea of unifying the different type classes.
> However, as far as I understand it, the semantics of structs and classes
> will still be different with this syntax. This may be a potential source
> of bugs, especially for people coming from C++:
>
> // 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;
> a = S(2);
> assert( b == a );
>
> // class references
> C a = new C(1);
> C& b = a;
> a = new C(2);
> assert( b == a ); // fails
>
> // .. same as
> C a = new C(1);
> C b = a;
> a = new C(2);
Yes, but this is not equivalent to what you were saying with structs, as you
are changing the underlying value of what the reference is pointing to. In
this case, you are just changing what the original reference is pointing to,
and so the old reference still exists intact.
-Steve
More information about the Digitalmars-d
mailing list