Why can't we define re-assignable const reference variable?

none z at gg.com
Sat Feb 16 15:37:12 PST 2008


== Quote from Janice Caron (caron800 at googlemail.com)'s article
> On 16/02/2008, none <z at gg.com> wrote:
> >  So why can't we have both (just as in C++):
> This has been covered so many times before, but in summary, allowing
> that would completely break the type system.

I don't understand why it will break the type system; in any case, even without it
(as we do now), I can always get around it by using D pointers:

  const(B)* b;            // type change
  b = &b1;                // rebind
  b = &b2;                // rebind

It's just so inconvenient, that's why I suggest allow define re-assignable const
reference variable.

> But I just want to clear up one misconception. You /cannot/ do that in
> C++. References in C++ are /never/ rebindable.

That's a terminology issue, to make it clear:

class C {}

D                           C++
=========================   ===========================
reference: C c = new C();   pointer:    C *c = new C();
pointer:   C* cp = &c;      ptr-to-ptr: C **cp = &c;


What I mean is in C++, you can define 2 different kinds of C++ pointers:

const C* b;  // b cannot be re-bind, and the object cannot be modified
C* const b;  // b can    be re-bind, but the object cannot be modified

So in D we should be able to define 2 different kinds of D reference:

const C b;  // b cannot be re-bind, and the object cannot be modified
C const b;  // b can    be re-bind, but the object cannot be modified



More information about the Digitalmars-d mailing list