mutable reference to const object

Vadim Lopatin coolreader.org at gmail.com
Thu Mar 6 05:17:05 PST 2014


Hello,

Is there a possibility to define mutable reference to const 
object?
I need variable which can be used to iterate through const 
objects.
But it seems like const(Foo)p makes constant reference to 
constant object instead of mutable reference to const object.

class Bar {
}
unittest {
    Bar bar1 = new Bar();
    Bar bar2 = new Bar();
    const(Foo) constref = bar1; // ok
    constref = bar2; // error - cannot modify const expression
}


In C++, following code works as I'm expecting:

class Bar {
}

Bar * ptr1 = new Bar();
Bar * ptr2 = new Bar();
const Bar * constptr = ptr1;
constptr = ptr2;


More information about the Digitalmars-d-learn mailing list