Could use some help with porting problems

bearophile bearophileHUGS at lycos.com
Tue Feb 7 19:52:07 PST 2012


Roderick Gibson:

> So my question is 1) How would I declare an array of pointers to 
> const(Class)? That is, how do I declare an array such that the ARRAY is 
> mutable, but the actual objects that it points to are not (ie you could 
> change the pointers in the array, but you cannot change anything in a 
> dereferenced object).

Currently one way to do it is:

import std.typecons;

class Foo { int x; }

void main() {
    Rebindable!(const(Foo))[] foos;
    foos.length = 5;
    foos[0] = new Foo();
    foos[0] = new Foo(); // OK
    foos[0].x = 5; // Error
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list