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

Janice Caron caron800 at googlemail.com
Tue Feb 19 14:27:38 PST 2008


On 19/02/2008, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> So please explain this:
>
> <snip>
> // outputs 2 2 1 2

Simple. If you wanted the output to be 2 2 2 2 then the second
function should have
been declared

    void assignit(ref C src, ref C value)

But you missed out the "ref"s, and so src was merely a local copy.


> In both cases, the assignit function is taking reference types as arguments.

But in the second case, the reference is /local/. Function parameters
declared without "ref" are just local variables. Assigning src will
modify the local copy (of the reference), but you need the "ref"
keyword if you want to modify the original (reference).


> Both do the same thing,

No, they don't. Passing a value by reference is not the same thing as
passing a reference by value.



More information about the Digitalmars-d mailing list