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

Janice Caron caron800 at googlemail.com
Wed Feb 20 12:40:40 PST 2008


On 20/02/2008, Sergey Gromov <snake.scaly at gmail.com> wrote:
>  As I understand from your posts, you state that "const(C) x;", as well
>  as "const C x;", declares a variable of type (const C).  But this is not
>  true.  It actually declares a variable of type "const ref to const C".

No. Consider

    const(int) x;

This does not declare a variable of type "const ref to const int", it
declares a const int.

I understand that you're thinking of classes, but even with classes,
you're not quite getting what a class is. A class is /not/ the data on
the heap. (If that were so, it would be possible to create an array of
dereferenced class instances, and it isn't). No, a class /is the
reference/ - and indirectly, what that reference points to. Thus, a
const class /is/ a const reference.

A "tailconst" class, on the other hand, would be a class (by which I
mean, a reference) whose tail (the data on the heap) was const.



More information about the Digitalmars-d mailing list